Skip to content

Commit 32bffdf

Browse files
CyberT33NCyberT33N
CyberT33N
authored and
CyberT33N
committed
feat(CCS-001): style changes for errorMiddleware
1 parent 064d6ae commit 32bffdf

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/middleware.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,45 +54,43 @@ export type IErrorMiddleware = (
5454
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5555
const errorMiddleware: IErrorMiddleware = (err, req, res, next) => {
5656
const {
57-
message, // <-- This is the error message from our custom error classes
57+
message,
5858
data,
5959
httpStatus,
6060
name,
6161
stack,
6262
timestamp,
63-
error // <-- This is the original error that caused the custom error
63+
error
6464
} = err
6565

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+
6769
const base: ICoreError = {
6870
name,
69-
environment: process.env.npm_lifecycle_event!,
71+
environment: npmLifecycleEvent!,
7072
timestamp: timestamp || new Date().toISOString(),
7173
message,
7274
httpStatus
7375
}
7476

75-
// Full error with error message and stack
7677
const fullError: ICoreError = {
7778
...base,
7879
error,
7980
data,
8081
stack
8182
}
8283

83-
// If npm_lifecycle_event is start we sanitize the error message and stacktrace
8484
const fullErrorSanitized: IErrorResponseSanitized = {
8585
...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()
9089
}
9190

9291
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)
9693
}
9794

95+
9896
export default errorMiddleware

0 commit comments

Comments
 (0)