Skip to content
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

Conversation

seantokuzo
Copy link
Contributor

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:

  • our custom error classes
  • errors thrown the old way (temporary until middlewares/controllers can be refactored to use custom error classes)
  • unknown errors by sending back a generic error response

The goal of this error handler is:

  • to consistently format error responses sent back to the client (with the help of our custom error class serializeErrors method): { message: string, field?: string }[]
  • to set relevant status codes
  • to log helpful information for debugging errors

This PR also:

  • has the app use the new global error handler
  • simplifies importing custom error classes from the errors folder (via 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

  • add - 3000:3000 to the ports array in docker-compose-dev.yml
  • npm run docker-dev
  • on a browser navigate to unhandled route - eg. http://localhost:3000/non-existent-route
  • expect our generic error message "Something went wrong, please try again later"

Checklist

All Team Members

  • I added a descriptive title to this PR.
  • I filled out the Description, Jira Task, and Testing Instructions sections above.
  • I added or updated [Jest unit tests]for any changes to components, server-side controllers, etc.
  • I ran npm run docker-test in my local environment to check that this PR passes all unit tests.
  • I did a quick check to make sure my code changes follow the recommended style guide.

Copy link
Contributor

@brok3turtl3 brok3turtl3 left a 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!

Comment on lines 10 to 25
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());
Copy link
Contributor

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 !

Comment on lines +1 to +8
export * from './badRequestError';
export * from './customError';
export * from './databaseConnectionError';
export * from './internalError';
export * from './notAuthorizedError';
export * from './notFoundError';
export * from './requestValidationError';
export * from './validationError';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good clean up

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);

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.

Copy link
Contributor Author

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

Copy link

@Mnelson98 Mnelson98 left a 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!

Copy link
Contributor

@JimmyTran48 JimmyTran48 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work!

@brok3turtl3
Copy link
Contributor

Good job @seantokuzo!

@brok3turtl3 brok3turtl3 merged commit 061e069 into CHE-167/story/BE-Refactor-Error-Handling Jun 10, 2024
1 check passed
@brok3turtl3 brok3turtl3 deleted the CHE-170/subtask/Refactor-Global-Error-Handler branch June 10, 2024 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants