[discuss] change how errors are handled
See original GitHub issueUpdate: 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:
- 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. - 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:
- 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. - 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 checkres.status
in the callbacks
Issue Analytics
- State:
- Created 10 years ago
- Comments:60 (23 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
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 theResponse
object, which has properties likestatus
andtext
. Anything that gets in the way of producing thatResponse
object is an error. Anything else is not.That’s why the signature is
err, res
. There are two possibilities: we goterr
, or we gotres
. We are proposing an in-between scenario where we gotres
, but it’s also anerr
.I’m still maintaining superagent-ls for the day sanity rules again 😉