question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should NetworkError extend TypeError?

See original GitHub issue

Is 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

https://github.com/mswjs/msw/blob/2e7ecd87e5568c6e59a408e812535f088498e437/src/mockServiceWorker.js#L210-L217

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, Feb 1, 2022

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 with window.fetch as it’s, effectively, a different client based on Fetch API.

1reaction
smeijercommented, Jan 20, 2022

Let’s check what class does the network error have when it happens without MSW around. We should adhere to that class in MSW as well.

So that would be TypeError. If you’d run a fetch while being offline or blocking the URL, the browser throw an Error of instance TypeError. 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.

fetch('/graphql', { method: 'POST' }).catch((e) => { 
  console.log('is TypeError', e instanceof TypeError);
})

https://user-images.githubusercontent.com/1196524/150381407-e033fb8c-5d82-4e6e-ad29-78392edbf1b1.mp4

Can you tell me more about when do you need this?

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found