25

If a fetch call fails in Chrome, then the only error details that I get back is

TypeError: Failed to fetch

How do I display an informative error message to the end user in this case?

Specifically:

Is it possible to get any details about why fetch failed?

For example, if the server crashed, Chrome DevTools might log a console message of net:ERR_EMPTY_RESPONSE, but there appears to be no way to access this from JavaScript.

As far as I can tell, the answer is no; I assume this is for security reasons, to avoid letting malicious JS find out which sites are and aren't accessible by inspecting error messages.

Is it possible to distinguish fetch errors from other TypeErrors?

If I can't get error details, I'd like to at least replace the horribly vague "Failed to fetch" with an informative "Failed to access the web site; please try again later" message, and I'd like to do this without any risk of displaying that message for other TypeErrors.

The only solution I've found here is to check the actual message to see if it's "Failed to fetch". This is obviously browser-specific; it works in Chrome, it seems like it will work in any user language of Chrome, and other browsers would need their own testing and handling.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • 8
    `TypeError` seems like...really the wrong error type. Strange. – T.J. Crowder Sep 08 '16 at 14:47
  • `.catch` doesn't work for you? – Jaromanda X Sep 08 '16 at 14:52
  • @JaromandaX - `.catch` works, but I'd like to get error details if possible (my first question), and to avoid incorrectly catching other `TypeError`s as fetch errors, I feel like I either have to put the `.catch` _very_ close to `fetch` (so no other errors can occur, thus losing flexibility in how I structure my promises) or rely on browser-specific behavior like the text of the fetch `message` (my second question). – Josh Kelley Sep 08 '16 at 15:06
  • I meant does handling the error in `.catch` stop the output to console (I take it that's where this error is showing) – Jaromanda X Sep 08 '16 at 15:09
  • The `TypeError: Failed to fetch` message is all you get in the `.catch` so you'll have to do some fancy footwork to change that to a meaningful message - the `.catch` can be at the end of the chain, doesn't matter – Jaromanda X Sep 08 '16 at 15:13

1 Answers1

8

It seems likely that there aren't any more details for networking/permission/input problems.

Is it possible to distinguish fetch errors from other TypeErrors?

Yes, you just need to catch only the the errors from the fetch call:

fetch(…)
.catch(err => new FetchError(err))
.…

class FetchError extends Error {
    constructor(orig) {
        super();
        this.message = "fetch error";
        this.details = orig;
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • It's also worth checking the protocol of the fetched url. Calling https:// on an http:// site can cause this error – Damian Green Jun 15 '17 at 12:31
  • Also, checking whether your `4XX` and `5XX` responses are not returning an `Access-Control-Allow-Origin: *` header could also be the cause. – James Marino Oct 10 '17 at 04:09