@@ -3,10 +3,13 @@ import { StatusCodes, getStatusMessage } from 'http-status-toolkit';
33import { isCustomAPIError } from './checking-custom-api-error' ;
44import { CustomAPIError } from './error' ;
55
6+
7+
68// Internal config object (optional override)
79let errorOptions = {
8- showStack : process . env . SHOW_STACK !== 'false' ,
9- logError : process . env . LOG_ERROR !== 'false' ,
10+ showStack : process . env . SHOW_STACK !== 'false' && process . env . NODE_ENV !== 'production' ,
11+ logError :
12+ process . env . LOG_ERROR !== 'false' && process . env . NODE_ENV !== 'production' ,
1013} ;
1114
1215export function setErrorOptions (
@@ -28,18 +31,16 @@ export interface ErrorResponse {
2831
2932export const globalErrorHandler = (
3033 err : unknown ,
31- req : Request ,
34+ _req : Request ,
3235 res : Response ,
33- next : NextFunction
36+ _next : NextFunction
3437) => {
3538 let statusCode : number = StatusCodes . INTERNAL_SERVER_ERROR ;
3639 let message = getStatusMessage ( StatusCodes . INTERNAL_SERVER_ERROR ) ;
3740 let errorDetails : string | object | null | undefined ;
3841 let stack : string | undefined ;
3942
4043
41- const isDev = process . env . NODE_ENV ?. trim ( ) === 'development' ;
42-
4344 if ( err instanceof Error ) {
4445 if ( isCustomAPIError ( err ) ) {
4546 const customErr = err as CustomAPIError ;
@@ -61,12 +62,19 @@ export const globalErrorHandler = (
6162 errorResponse . errorDetails = errorDetails ;
6263 }
6364
64- if ( isDev && stack && errorOptions . showStack ) {
65+ if ( stack && errorOptions . showStack ) {
6566 errorResponse . stack = stack . split ( '\n' ) . map ( ( line ) => line . trim ( ) ) ; ;
6667 }
6768
68- if ( isDev && errorOptions . logError ) {
69- console . error ( err ) ;
69+ // Log the error if configured to do so
70+ if ( errorOptions . logError ) {
71+ console . error ( '\x1b[35m%s\x1b[31m' , 'Error Message:' , errorResponse . message ) ;
72+ if ( errorResponse . errorDetails ) {
73+ console . error ( 'Error Details:' , errorResponse . errorDetails ) ;
74+ }
75+ if ( errorResponse . stack ) {
76+ console . error ( '\x1b[35m%s\x1b[32m' , 'Stack Trace:' , errorResponse . stack ) ;
77+ }
7078 }
7179
7280 res . status ( statusCode ) . json ( errorResponse ) ;
0 commit comments