This repository has no description
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// SPDX-License-Identifier: AGPL-3.0-or-later
import {HttpStatus} from '@fluxer/constants/src/HttpConstants';import {createJsonErrorResponse, createXmlErrorResponse} from '@fluxer/errors/src/error_handling/ErrorResponse';import {FluxerError} from '@fluxer/errors/src/FluxerError';import type {Context, ErrorHandler} from 'hono';import {HTTPException} from 'hono/http-exception';
const HTTP_STATUS_TO_ERROR_CODE: Record<number, string> = { 400: 'BAD_REQUEST', 403: 'FORBIDDEN', 404: 'NOT_FOUND', 405: 'METHOD_NOT_ALLOWED', 409: 'CONFLICT', 410: 'GONE', 500: 'INTERNAL_SERVER_ERROR', 501: 'NOT_IMPLEMENTED', 502: 'BAD_GATEWAY', 503: 'SERVICE_UNAVAILABLE', 504: 'GATEWAY_TIMEOUT',};
export type ResponseFormat = 'json' | 'xml';
export interface ErrorHandlerOptions { logError?: (error: Error, context: Context) => void; includeStack?: boolean; responseFormat?: ResponseFormat; customHandler?: (error: Error, context: Context) => Response | Promise<Response> | undefined;}
export function createErrorHandler(options: ErrorHandlerOptions = {}): ErrorHandler { const {logError, includeStack = false, responseFormat = 'json', customHandler} = options; return async (error: Error, c: Context): Promise<Response> => { if (logError) { logError(error, c); } if (customHandler) { const customResponse = await customHandler(error, c); if (customResponse) { return customResponse; } } if (error instanceof FluxerError) { return error.getResponse(); } if (error instanceof HTTPException) { const status = error.status; const code = HTTP_STATUS_TO_ERROR_CODE[status] ?? 'GENERAL_ERROR'; const message = error.message || 'An error occurred'; if (responseFormat === 'xml') { return createXmlErrorResponse(status, code, message); } return createJsonErrorResponse({ status, code, message, data: includeStack ? {stack: error.stack} : undefined, }); } const status = HttpStatus.INTERNAL_SERVER_ERROR; const message = includeStack ? error.message : 'Something went wrong. Please try again later.'; if (responseFormat === 'xml') { return createXmlErrorResponse(status, 'INTERNAL_SERVER_ERROR', message); } return createJsonErrorResponse({ status, code: 'INTERNAL_SERVER_ERROR', message, data: includeStack ? {stack: error.stack} : undefined, }); };}