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?
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.)
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".
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:
I guess we already have the name AbortError taken with the new abortable fetch thing.
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.
@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 why would it be hard to switch from TypeError? Do we think folks are looking at the type currently?
@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.
@mattto is there any way we can get data on that?
Alternatively, we could give priority to FetchObserver and put the data there.
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
@domenic is someone still pursuing ways to add custom data to errors somehow in TC39?
Sadly no.
@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 agreed. This is why I was surprised @annevk said it'd be difficult to switch. Although it seems difficult/impossible to know for sure.
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.
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.
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:
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.
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.
Most helpful comment
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 inspectingerror.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.