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

Unclear how to throw an actual error #579

Closed
theoephraim opened this issue Jun 12, 2024 · 3 comments · Fixed by #589
Closed

Unclear how to throw an actual error #579

theoephraim opened this issue Jun 12, 2024 · 3 comments · Fixed by #589
Assignees

Comments

@theoephraim
Copy link

It seems all errors thrown in an interceptor are swallowed and turned into a request responses.

What if I actually just want to cancel the request altogether and throw an error? Is that even possible? I did some digging in the source and the docs but couldn't quite figure it out.

Thanks!

@theoephraim
Copy link
Author

theoephraim commented Jun 13, 2024

I figured it out - see #566

For anyone else coming across this, you must attach an unhandledException event listener, and then you can re-throw the error, or do whatever else you need to... In my case, wanting to throw the error, that looks like this:

interceptor.on('unhandledException', ({ error, request, requestId, controller }) => { throw error; });

I suppose the issue still stands that this was not clear, as it it does not seem to be documented. I will try to find some time to open a PR.

@kettanaito
Copy link
Member

Hi, @theoephraim. That's certainly an oversight on my end.

Please, could you open a PR that adds a new section called "Error handling" after this block in the readme?

It would be great to mention the following things in that new section:

  1. How by default unhandled exceptions are coerced to 500 error responses.
  2. You can still listen to exceptions by providing the unhandledException event listener.
  3. If you do provide the listener, and that listener handles the request or throws the exception, it won't be coerced to 500 response. This allows you to opt-out from the default behavior (show two examples: 1 handles exceptions with a mocked response; 2 throws the error).

@kettanaito
Copy link
Member

Released: v0.30.1 🎉

This has been released in v0.30.1!

Make sure to always update to the latest version (npm i @mswjs/interceptors@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 a pull request may close this issue.

2 participants