-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CHE-170] Refactor Global Error Handler #138
[CHE-170] Refactor Global Error Handler #138
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all looking good - Excited to see it roll out across the BE!
server/middleware/errorHandler.ts
Outdated
const errorHandler = (err: Error | OldError, _req: Request, res: Response, _next: NextFunction) => { | ||
// If it is one of our custom errors use its properties/method | ||
if (err instanceof CustomError) { | ||
console.log(err.message); | ||
return res.status(err.statusCode).send(err.serializeErrors()); | ||
} | ||
|
||
// Handle errors thrown the old way | ||
if (!(err instanceof Error) && err.status && err.log && err.message) { | ||
console.log(err.log); | ||
return res.status(err.status).send([{ message: err.message.err }]); | ||
} | ||
|
||
// If it is an unknown error send back a generic error | ||
const internalError = new InternalError(); | ||
return res.status(internalError.statusCode).send(internalError.serializeErrors()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great transitional logic here @seantokuzo !
export * from './badRequestError'; | ||
export * from './customError'; | ||
export * from './databaseConnectionError'; | ||
export * from './internalError'; | ||
export * from './notAuthorizedError'; | ||
export * from './notFoundError'; | ||
export * from './requestValidationError'; | ||
export * from './validationError'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good clean up
server/middleware/errorHandler.ts
Outdated
const errorHandler = (err: Error | OldError, _req: Request, res: Response, _next: NextFunction) => { | ||
// If it is one of our custom errors use its properties/method | ||
if (err instanceof CustomError) { | ||
console.log(err.message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me! I'm curious if 'console.error' would be more fitting here, however? Maybe not, just wondering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! console.error
is def more appropriate.
I think I also need to be logging the entire err to get the stack trace for debugging. Refactor coming in hot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work!
Good job @seantokuzo! |
061e069
into
CHE-167/story/BE-Refactor-Error-Handling
Description
As Part of the CHE-167 story to refactor error handling, this PR aims to:
Create and use a global error handler middleware that handles:
The goal of this error handler is:
serializeErrors
method):{ message: string, field?: string }[]
This PR also:
server/errors/index.ts
)Jira Task
JIRA TICKET
Testing Instructions
(Make sure you have the most recent image by running
npm run docker-remove-all
first)Check if current tests still pass with:
npm run docker-test:all
Make sure error handler is working
- 3000:3000
to the ports array in docker-compose-dev.ymlnpm run docker-dev
Checklist
All Team Members
npm run docker-test
in my local environment to check that this PR passes all unit tests.