@@ -54,45 +54,43 @@ export type IErrorMiddleware = (
54
54
// eslint-disable-next-line @typescript-eslint/no-unused-vars
55
55
const errorMiddleware : IErrorMiddleware = ( err , req , res , next ) => {
56
56
const {
57
- message, // <-- This is the error message from our custom error classes
57
+ message,
58
58
data,
59
59
httpStatus,
60
60
name,
61
61
stack,
62
62
timestamp,
63
- error // <-- This is the original error that caused the custom error
63
+ error
64
64
} = err
65
65
66
- // Base will be always there does not matter which npm_lifecycle_event
66
+ const npmLifecycleEvent = process . env . npm_lifecycle_event
67
+ const isSanitized = npmLifecycleEvent === 'start'
68
+
67
69
const base : ICoreError = {
68
70
name,
69
- environment : process . env . npm_lifecycle_event ! ,
71
+ environment : npmLifecycleEvent ! ,
70
72
timestamp : timestamp || new Date ( ) . toISOString ( ) ,
71
73
message,
72
74
httpStatus
73
75
}
74
76
75
- // Full error with error message and stack
76
77
const fullError : ICoreError = {
77
78
...base ,
78
79
error,
79
80
data,
80
81
stack
81
82
}
82
83
83
- // If npm_lifecycle_event is start we sanitize the error message and stacktrace
84
84
const fullErrorSanitized : IErrorResponseSanitized = {
85
85
...base ,
86
- data : process . env . npm_lifecycle_event === 'start' ? SanitizedMessage . DEFAULT : data ,
87
- stack : process . env . npm_lifecycle_event === 'start' ? SanitizedMessage . DEFAULT : stack ,
88
- // We must use toString() because error can be an instance of Error
89
- error : process . env . npm_lifecycle_event === 'start' ? SanitizedMessage . DEFAULT : error ?. toString ( )
86
+ data : isSanitized ? SanitizedMessage . DEFAULT : data ,
87
+ stack : isSanitized ? SanitizedMessage . DEFAULT : stack ,
88
+ error : isSanitized ? SanitizedMessage . DEFAULT : error ?. toString ( )
90
89
}
91
90
92
91
console . error ( '[ErrorManager] Full Error: ' , fullError )
93
-
94
- const status = httpStatus ?? StatusCodes . INTERNAL_SERVER_ERROR
95
- res . status ( status ) . json ( fullErrorSanitized )
92
+ res . status ( httpStatus ?? StatusCodes . INTERNAL_SERVER_ERROR ) . json ( fullErrorSanitized )
96
93
}
97
94
95
+
98
96
export default errorMiddleware
0 commit comments