Should NetworkError extend TypeError?
See original GitHub issueIs your feature request related to a problem? Please describe.
A fetch()
promise will reject with a TypeError when a network error is encountered or CORS is misconfigured on the server-side (mdn, but MSW throws a custom type NetworkError
, which extends Error
. This makes it impossible to test error handling depending on instanceof TypeError
checks to handle for network errors explicitly.
catch (error) {
if (error instanceof TypeError) return doSomething()
throw error;
}
Describe the solution you’d like
I think NetworkError
should extend the more specific TypeError
instead of Error
.
export class NetworkError extends TypeError {
constructor(message: string) {
super(message)
this.name = 'NetworkError'
}
}
Describe alternatives you’ve considered Add an option to throw specific kinds of errors.
ctx.throw(new TypeError('something went wrong'))
Additional context https://github.com/mswjs/msw/blob/2e7ecd87e5568c6e59a408e812535f088498e437/src/utils/NetworkError.ts#L1-L6
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
TypeError : NetworkError when attemptimg to fetch a resource
I'm trying to have my Firefox extension send/recieve data to/from a spring boot app running on localhost:8080/ and I am getting this error....
Read more >Handling operation errors - Apollo GraphQL Docs
When a network error occurs, Apollo Client adds it to the error.networkError field returned by your useQuery call (or whichever operation hook you...
Read more >Built-in Exceptions — Python 3.11.1 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. ... references or attribute assignments at all, TypeError is...
Read more >[Fetch] "NetworkError when attempting to fetch resource ...
Fetch requests error out with "TypeError: NetworkError when attempting to fetch resource" whenever the user navigates away while there is an ongoing async ......
Read more >Errors | Node.js v19.3.0 Documentation
Stack traces extend only to either (a) the beginning of synchronous code ... Node.js will generate and throw TypeError instances immediately as a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey, @smeijer. Zero time has been wasted! It was a great opportunity for me to refresh my knowledge of client errors. Yeah, in the end, each request client is in charge of what errors it produces. This also makes sense that
node-fetch
would have a difference withwindow.fetch
as it’s, effectively, a different client based on Fetch API.So that would be
TypeError
. If you’d run a fetch while being offline or blocking the URL, the browser throw an Error of instanceTypeError
. That’s the use case I need to be able to reproduce, so I can inform the user that something is wrong with the network, and not so much with the entered form data or our server.https://user-images.githubusercontent.com/1196524/150381407-e033fb8c-5d82-4e6e-ad29-78392edbf1b1.mp4
I don’t need the option to throw any other errors than
TypeError
to be honest. The issue template asked about alternatives, so that would be one. Feel free to ignore that.