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

Added feature: res.networkError() mocks a network error #253

Merged
merged 8 commits into from
Jul 15, 2020
Merged

Added feature: res.networkError() mocks a network error #253

merged 8 commits into from
Jul 15, 2020

Conversation

jensmeindertsma
Copy link

@jensmeindertsma jensmeindertsma commented Jun 29, 2020

I added the feature so you can now use res.networkError() inside a request handler to mock a network request. This is useful because this will trigger the Fetch API to throw an error, and you can thus test whether your function handles this error correctly.

GitHub

@kettanaito kettanaito added this to the 0.20.0 milestone Jul 1, 2020
@jensmeindertsma jensmeindertsma marked this pull request as draft July 1, 2020 15:02
@kettanaito
Copy link
Member

Hey. I've looked into your implementation and it looks good!

What happens now is that whenever you use res.networkError() it rejects a request promise and results into an unhandled error in the browser's console. However, that doesn't disrupt the request resolution, and since nothing has been returned from the handler, the request gets performed as-is. This also results into a Network error (considering you're requesting a non-existing resource).

I believe we need to find a way to cancel a pending request (or respond with a network error from the worker/node), so it doesn't get executed. res.networkError() is, effectively, a short-circuit for a request.

@kettanaito
Copy link
Member

I have some new information on how to properly handle an exception thrown within a request handler. Will update soon, it's a change to node-request-interceptor.

@kettanaito
Copy link
Member

Hey. I've rebased your feature branch against the latest master that should contain the changes necessary to treat exceptions in request handlers as network errors. Will revisit tests and CI.

@jensmeindertsma
Copy link
Author

Nice! I'm very busy this weekend but I'll try to take a look later.

@kettanaito
Copy link
Member

I've updated the branch, and added the logic that treats the exception thrown in res.networkError() as an intended exception. Previously, we've treated any exception originating from a request handler as unintended (what if your function suddenly throws? that's most likely an issue with your function). With the introduction of network errors, we have a different group of intended exceptions now.

It's possible to emulate a network error on the Service Worker side by rejecting the pending request promise. Once done, it produces the very same errors as a regular network error.

The only thing I couldn't get to is asserting the custom network error message in the browser test. Somehow both fetch rejection and captured console errors don't have it.

@kettanaito kettanaito marked this pull request as ready for review July 15, 2020 08:31
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.

This looks awesome. I'm excited to ship res.networkError feature in the next release.

Superb job, @jensmeindertsma! Thank you and welcome to contributors 🎉

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.

Request handler should allow returning undefined or void
2 participants