question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling HTTP error statuses

See original GitHub issue

The 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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
dgrahamcommented, Jan 9, 2016

The promises tutorial describes the complexities of asynchronous error handling well.

The second argument to then is an error handling function that is invoked when the promise is rejected, as with a fetch network error.

var done = (response) => console.log(response)
var fail = (error) => console.log('fail', error)
Promise.reject(42).then(done, fail).catch(e => console.log('catch', e))
// => fail 42

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.

done = (response) => { throw 'boom' }
Promise.resolve(42).then(done, fail).catch(e => console.log('catch', e))
// => catch boom

So we can use the second argument to then to 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!

1reaction
richardscarrottcommented, Jan 9, 2016

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:

var req = fetch()
    .then(checkStatus)
    .then(parseJson);

req.then(done);
req.catch(fail);

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found