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.

[discuss] change how errors are handled

See original GitHub issue

Update: This thread is now a discussion about whether to revert the behavior where all non-2xx responses are treated as errors. Please read the comments before just posting a +1 or -1 or other comment.

The arity change is not up for discussion and no one is disagreeing with that one.


Currently errors are handled in the following ways:

  1. If the function to a .get or a .end as an arity of two, the first will be an error and the second a response. If it has an arity of one then only the response is provided.
  2. In the case of non-200 responses, no error is emitted or set and the user must check res.status

I would like to discuss the following changes:

  1. No more magic for the callback. Always require an error argument to make things simpler and more obvious at the callsites. The returned “request” object from .get or .end could still emit error to allow for some “centralize” handling.
  2. All non-200 responses are treated as errors. This means that we detect the response status and make an error object with a .status field as needed. We do no other response processing like detecting an error message or whatever (leaving that up to the user). In the case of some network error or other xmlhttprequest error (the current error types) we don’t set a .status since there wouldn’t be one.

The above brings a few advantages in my mind (yes the changes would be major version bump breaking).

  • less ambiguous function calls by removing magic arity use
  • simpler error handling at the callsite since for ajax requests, any non 2xx is a non-success and therefore an error. Making the code to act on success simpler in the common case (currently one has to constantly check res.status in the callbacks

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:60 (23 by maintainers)

github_iconTop GitHub Comments

2reactions
rauchgcommented, Mar 26, 2015

They simply are not errors. They’re data. That’s why you’re always gonna have edge cases or debates about whether X status code was an error or not. The data got from end to end successfully. It was a success case, not an error case.

Think about it this way. The goal of the request function is to get the Response object, which has properties like status and text. Anything that gets in the way of producing that Response object is an error. Anything else is not.

That’s why the signature is err, res. There are two possibilities: we got err, or we got res. We are proposing an in-between scenario where we got res, but it’s also an err.

1reaction
kmalakoffcommented, Oct 21, 2015

I’m still maintaining superagent-ls for the day sanity rules again 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

[discuss] change how errors are handled · Issue #283 - GitHub
Update: This thread is now a discussion about whether to revert the behavior where all non-2xx responses are treated as errors.
Read more >
2.5 Error Handling and Generation - Bookdown
There are a few essential functions for generating errors, warnings, and messages in R. The stop() function will generate an error. Let's generate...
Read more >
A Definitive Guide to Handling Errors in JavaScript - Kinsta
This article will guide you through the basic errors in JavaScript and explain the various errors you might encounter.
Read more >
Thoughtful Error Handling - Medium
Any time a value in your application may change is a point where an error can occur. The type of error will depend...
Read more >
Improper Error Handling - OWASP Foundation
A specific policy for how to handle errors should be documented, including the types of errors to be handled and for each, what...
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