good luck catching fetch redirection ssl error

Have you ever tried fetching a resource that would redirect to an HTTPS url whose SSL certificate is not recognised by your browser (or downright invalid)?

Yep, of course the fetch will fail (in general). But how to catch it properly?

Instinctively, one will probably think of something along this line:

fetch(...).then(...).catch((err) => {
    // your catching logic here
})

which is fair enough. But when inspecting the err, you may find that it simply logs out:

TypeError: Failed to fetch

Chrome inspector does show this message up in its error log (in red):

net::ERR_CERT_INVALID

but that’s about it. Worse yet, there is no response object to inspect further, as the response is only available in the then block (when the fetch operation actually succeeds).

So, if you are like me, and you’re interested in a way to programmatically handle this error (e.g. if it’s an SSL certificate error, then show a less cryptic error message to the end-user), I hate to bring sad news that I haven’t found any possible way so far. That, or there’s currently no accessible documentation on this topic out there.

I’m so grateful if someone could help shed some light on this!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s