Have you ever tried fetch
ing 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!