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

feat: gracefully handle middleware exceptions #43

Merged
merged 1 commit into from
Apr 5, 2024

Conversation

christoph-fricke
Copy link
Contributor

@christoph-fricke christoph-fricke commented Apr 5, 2024

This PR prevents HTTP servers from crashing when they use this middleware and an error is thrown in some MSW handler. Now, the error can be handled by the server's error middleware instead.

Background:
We have a test setup with MSW handlers that use @mswjs/data and query the DB with strict queries. Therefore, it may happen that a handler throws an error. Yesterday, we started to this package to reuse our handlers and DB setup for local development as well.

We noticed that the whole server process crashes when @mswjs/data throws an error instead of handling it with the default error middleware built into Express. This happens because Express is not able to handle Promises, yet. Apparently, it is only able to catch errors in synchronous code automatically. Promise rejections have to be passed to the next argument explicitly. See: https://expressjs.com/en/guide/error-handling.html

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thank you for improving this library! 🚀

@kettanaito kettanaito changed the title feat: gracefully handle promise rejections feat: gracefully handle middleware exceptions Apr 5, 2024
@kettanaito kettanaito merged commit 31bc709 into mswjs:main Apr 5, 2024
1 check passed
@christoph-fricke christoph-fricke deleted the feat/error-handling branch April 5, 2024 13:41
@kettanaito
Copy link
Member

Released: v0.10.0 🎉

This has been released in v0.10.0!

Make sure to always update to the latest version (npm i @mswjs/http-middleware@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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.

2 participants