fetch icon indicating copy to clipboard operation
fetch copied to clipboard

More informative error types

Open jakearchibald opened this issue 8 years ago • 21 comments

Just had a chat with an internal team requesting more detailed errors, so they can tell the difference between a timeout / connection failure / bad params etc etc.

Given that we're going to be introducing AbortError, should we revisit other errors?

jakearchibald avatar Apr 13 '17 16:04 jakearchibald

To some extent there's #352.

And then there's the question of what exactly resource timing (I believe that's the one, not sure) exposes, when, and how many user agents have decided that's an acceptable level of exposure.

If we have those details, we could consider it. (Then there's the problem of potential compatibility fallout, that we can only measure through telemetry (or maybe not) or we add information to TypeError instances which is rather hackish.)

annevk avatar Apr 13 '17 16:04 annevk

This is also an issue in react-native because they use what-wg fetch. Is there a way to differentiate between timeout or offline? Currently both situation throw the same error which from my memory is something like "NetworkRequest failed".

https://stackoverflow.com/q/48847068/1828637

Noitidart avatar Feb 23 '18 23:02 Noitidart

A way forward may be to make a list of desired error types and see which ones are OK to expose cross-origin or not. Here's a strawman:

  • "API error" (gave something that's not a Request or a URL to fetch())
  • "network error" (something at the TCP/IP layer caused us to fail to fetch
  • "abort error" (the user closed the tab or clicked stop or something like that)
  • "timeout error" (the browser timed out the request (I'm not sure this really happens for fetch()))
  • "offline error" (this is a subset of network error when the browser knows it's offline)

I guess we already have the name AbortError taken with the new abortable fetch thing.

mfalken avatar Apr 12 '18 05:04 mfalken

Most of those are probably reasonable (assuming "offline error" matches navigator.onLine). @domenic is someone still pursuing ways to add custom data to errors somehow in TC39? It seems hard to switch away from TypeError here.

annevk avatar Apr 12 '18 06:04 annevk

@mattto per https://github.com/whatwg/xhr/issues/96 WebKit at least has a network timeout. I suspect that applies to all browsers to some extent.

annevk avatar Apr 12 '18 06:04 annevk

@annevk why would it be hard to switch from TypeError? Do we think folks are looking at the type currently?

jakearchibald avatar Apr 12 '18 09:04 jakearchibald

@jakearchibald I'd expect so; we certainly do so throughout the test suite. I know it's not common practice, but it's also rather hard to guarantee that nobody is doing it.

annevk avatar Apr 12 '18 09:04 annevk

@mattto is there any way we can get data on that?

Alternatively, we could give priority to FetchObserver and put the data there.

jakearchibald avatar Apr 12 '18 09:04 jakearchibald

I imagine it'd be hard to get data on that. There's no clear API entry point we could add a UseCounter for. We could probably do a search on HTTPArchive to try to find sites that do this. What would the script code look like? Something like this? fetch(...).catch(err => { err instanceof TypeError; })

cc @yutakahirano

mfalken avatar Apr 12 '18 12:04 mfalken

@domenic is someone still pursuing ways to add custom data to errors somehow in TC39?

Sadly no.

domenic avatar Apr 12 '18 13:04 domenic

@mattto

fetch(...).catch(err => { err instanceof TypeError; })

I feel it very strange because currently TypeError is the only error type fetch throws (except for AbortError, right, but it's still not very popular I guess...). The only usecase I can image except for testing is checking whether the error is thrown by fetch, together with statements that may throw errors other than TypeError, so that should be more complicated. Also, we need to care about .name, I think.

yutakahirano avatar Apr 13 '18 04:04 yutakahirano

@yutakahirano agreed. This is why I was surprised @annevk said it'd be difficult to switch. Although it seems difficult/impossible to know for sure.

jakearchibald avatar Apr 13 '18 08:04 jakearchibald

This discussion aligns pretty closely with the Network Error Logging spec, which would allow the user agent to upload reports about these kinds of failures to the server owner. NEL currently defines a list of error types (a glorified string enum), but if that list were part of fetch, we wouldn't have to.

dcreager avatar Jun 05 '18 13:06 dcreager

What's the status of this? This would be very helpful to distinguish being offline from real errors - to help catch bad configurations, bugs in the service worker, etc.

aaronsn avatar Jun 06 '19 21:06 aaronsn

Nobody is working on this, though it seems like necessary infrastructure, e.g., for Network Error Logging. If someone wants to take this on, there are roughly three parts here:

  1. Identify all the places where we create a "network error" today, annotate them with a private slot regarding the type of error and ensure that annotation makes it all the way to the caller. (E.g., there might be places where we return a new network error at some point as currently there is no distinction.) This should not be too hard, although when we get to the HTTP layer there might be some difficulty in identifying all the appropriate types. (Perhaps Network Error Logging can help.)
  2. Figure out what from 1 we can reasonably expose to JavaScript, without giving attackers more information than they have today.
  3. Figure out out how to expose what remains in 2 to JavaScript.

annevk avatar Jun 07 '19 18:06 annevk

I still think a fetch observer is the right way to expose it, but yeah, steps 1 & 2 can be done before we decide on that.

jakearchibald avatar Jun 28 '19 08:06 jakearchibald

Hi all, I am one of the maintainers of Ky, which is a fetch-based library for HTTP requests. We would like the ability to differentiate fetch errors from other types of errors, including implementation errors from our users. This currently seems to require inspecting error.message, which is full of problems. Among other things, different browsers throw errors with different messages for the same situation. It's very messy. We would very much appreciate any improvements that could be made here.

sholladay avatar Nov 16 '19 04:11 sholladay

Any news?

RealAlphabet avatar Mar 07 '23 12:03 RealAlphabet

There is now https://github.com/sindresorhus/is-network-error, a bandaid solution that matches on the returned error strings, which all implementations handle differently.

This is probably not easily fixable from a spec perspective because code in the wild relies on the existing TypeError class, so one can't just introduce a NetworkError class now.

silverwind avatar Sep 26 '23 19:09 silverwind

Unfortunately I missed the nuance in the more recent replies that are not asking for more granularity but instead are asking for distinguishing the exception when you are further away from the caller. I suppose one thing we could do is try to standardize the message value. Looking at https://github.com/sindresorhus/is-network-error/blob/main/index.js Chromium's "Failed to fetch" seems pretty reasonable.

That's not a great solution, but it's an improvement.

I'm not entirely sure what a great solution would look like.

annevk avatar Sep 28 '23 08:09 annevk

I came across this issue today and had to come up with a best-effort approach. I tweeted about it:

I can't find a reliable way to detect retryable connection/network errors using fetch across @nodejs, @bunjavascript, @googlechrome and other platform. It's really unfortunate that the spec authors landed on throwing TypeError for pretty much anything that can go wrong. This includes CORS and CSP violations on the browser which are not retryable and those errors are opaque to JS code. DOMException is already used by AbortSignal and could have been a good choice since it's .name property reflects an error code. An explicit FetchError would have been even better still. Playground link

image

Unfortunately I cannot distinguish CSP/CORS-related errors from actual network errors using this code but it’s as close as I can get.

disintegrator avatar Nov 08 '23 01:11 disintegrator