When I make a request to a Node server from the client using whatwg-fetch
and I get a non 200 response, I'd like to be able to catch
that error and return a custom response.
For example:
const makeRequest = (endpoint) => {
try {
return fetch(`/api${endpoint}`, requestConfig)
.catch(e => console.warn(e))
.then(res => res.json())
.catch(e => console.warn(e))
} catch(e) {
// Never logs...
console.warn(e);
}
}
When I get a 502 or 404, for example, I'm still get a console.error in the console...
In Chrome console when I click to view the source of the error:
I don't understand why, if fetch()
caused the error, why wrapping it in a try/catch doesn't catch this error?
How can I catch
the errors from fetch()
for a non-200 response?