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

PIMS-1328 Express Error Handling #2197

Merged
merged 15 commits into from
Feb 21, 2024
Merged

PIMS-1328 Express Error Handling #2197

merged 15 commits into from
Feb 21, 2024

Conversation

dbarkowsky
Copy link
Collaborator

@dbarkowsky dbarkowsky commented Feb 16, 2024

🎯 Summary

PIMS-1328

Changes

  • Added a fallback route handler that returns 404 if that route doesn't exist.
  • Added some preset error responses we can use if wanted.
  • Added error handler middleware to catch errors and send back a formatted response.
    • The next() function accepts string, Error, and ErrorWithCode.
  • Added some process listeners that will close the app in a controlled manner, closing existing connections first.

Testing

  • Easiest way to test is to add a temporary endpoint in express.ts (I used /error). This endpoint can then just throw different kinds of errors to see different responses.
app.use('/error', (req: Request, res: Response) => {
  throw new Error('hi')
})
  • Recommended to use next() instead, as it also allows this to work with async controllers.
app.use('/error', async (req: Request, res: Response, next: NextFunction) => {
  next(new Error('hi'))
})

or even just

app.use('/error', async (req: Request, res: Response, next: NextFunction) => {
  next('hi')
})
  • Test the SIGTERM one by using this somewhere
app.use('/error', async (req: Request, res: Response, next: NextFunction) => {
  process.kill(process.pid, 'SIGTERM')
})
  • Test the non-existent route by calling a route that doesn't exist (e.g. /help)

🔰 Checklist

  • I have read and agree with the following checklist and am following the guidelines in our Code of Conduct document.
  • I have performed a self-review of my code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation where required.
  • I have tested my changes to the best of my ability.
  • My changes generate no new warnings.

Copy link

🚀 Deployment Information

The API Image has been built with the tag: 2197. Please make sure to utilize this specific tag when promoting these changes to the TEST and PROD environments during the API deployment. For more updates please monitor Image Tags Page on Wiki.

Copy link

codeclimate bot commented Feb 16, 2024

Code Climate has analyzed commit 323d045 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 91.1%.

View more on Code Climate.

Copy link
Collaborator

@TaylorFries TaylorFries left a comment

Choose a reason for hiding this comment

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

Bad endpoints caught and SIGTERM handled well. App never crashed!

@dbarkowsky dbarkowsky merged commit 9dccd98 into main Feb 21, 2024
5 checks passed
@dbarkowsky dbarkowsky deleted the PIMS-1328-API-Error-Handler branch February 21, 2024 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants