superagent: [discuss] change how errors are handled
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:
- 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
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 60 (23 by maintainers)
Commits related to this issue
- make any non-successful HTTP response result in an error argument Any completed response (non network error) also results in the error argument being provided to the end callback. This make it easier... — committed to ladjs/superagent by defunctzombie 9 years ago
- make any non-successful HTTP response result in an error argument Any completed response (non network error) also results in the error argument being provided to the end callback. This make it easier... — committed to ladjs/superagent by defunctzombie 9 years ago
- make any non-successful HTTP response result in an error argument Any completed response (non network error) also results in the error argument being provided to the end callback. This make it easier... — committed to ladjs/superagent by defunctzombie 9 years ago
- make any non-successful HTTP response result in an error argument Any completed response (non network error) also results in the error argument being provided to the end callback. This make it easier... — committed to ladjs/superagent by defunctzombie 9 years ago
- Reverted changes from https://github.com/visionmedia/superagent/pull/554 and https://github.com/visionmedia/superagent/issues/283 — committed to kmalakoff/superagent-ls by deleted user 9 years ago
- make any non-successful HTTP response result in an error argument Any completed response (non network error) also results in the error argument being provided to the end callback. This make it easier... — committed to Morita0711/npm-superurgent by Morita0711 9 years ago
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
.4xx and 5xx are totally errors. The majority of the time you don’t want them.
I’m still maintaining superagent-ls for the day sanity rules again 😉