Handling HTTP error statuses
See original GitHub issueThe docs recommend handling HTTP error statuses by throwing an error so the consumer can handle them within Promise.prototype.catch() – https://github.com/github/fetch/tree/v0.10.1#handling-http-error-statuses
Whilst this is more familiar to people experienced with jQuery.ajax I’m not sure it’s the best approach as, unlike jQuery.ajax, window.fetch uses es6 promises meaning runtime errors in the success handler will result in the catch handler being called. e.g.
window.fetch('https://api.github.com/users/richardscarrott/repos')
.then(checkStatus) // throws if status is outside 200-299
.then(parseJSON)
.then(function success(data) {
console.log('request succeeded with JSON response', data);
data.foo.bar; // throws as foo is undefined
}).catch(function error(error) {
console.log('request failed', error); // error could either be a network or a runtime error
});
I’m struggling to see how es6 promises can be used to express the semantics between a network error and a runtime error…
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >Best Practices for REST API Error Handling - Baeldung
2. HTTP Status Codes · 100-level (Informational) – server acknowledges a request · 200-level (Success) – server completed the request as expected ...
Read more >How to Handle HTTP Status Codes When Consuming a REST ...
How to Handle HTTP Status Codes When Consuming a REST API in Low-Code · Approach #1: Default Behavior · Approach #2: Handling the...
Read more >How to handle HTTP status codes? - ios - Stack Overflow
There's actually no need to check for each and every status codes. What I would normally do is to check if the request...
Read more >HTTP Status Codes: All 63 explained - including FAQ & Video
HTTP status codes are three-digit responses from the server to the browser-side request. Everyone has probably gotten the classic 404 page-not-found error.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The promises tutorial describes the complexities of asynchronous error handling well.
The second argument to
thenis an error handling function that is invoked when the promise is rejected, as with a fetch network error.Notice the catch handler was not invoked, because the second argument handled the error. If we didn’t provide the fail function, the error would fall through into the catch handler.
When the promise is resolved with a fetch response, and the success handler throws an exception while handling the response, the fail function is not invoked, but the catch handler is.
So we can use the second argument to
thento deal with fetch network errors, and we can use the catch handler to deal with rendering errors.The simple case, where we just handle successful fetches in
then, and allow a catch block to handle network errors, non-200 responses, and bugs in 200 response handling code, is sufficient when we want to show the user a “This didn’t work. Please try again.” message regardless of its cause.Hope that helps!
To be honest I had never considered
.then(done, fail)behaving differently to.then(done).catch(fail)but now you’ve explained it, it makes sense and is exactly what I’m after – it would be the same as doing:Personally I feel it would be better for the docs to show an example with
.then(done, fail)to demonstrate handling network errors codes but perhaps thats just my preference.Thanks for your help!