sheaivey / react-axios

Axios Components for React with child function callback
MIT License
180 stars 20 forks source link

Pass response on error when available #18

Closed binki closed 6 years ago

binki commented 7 years ago

For many error conditions, such as server reporting failure in its response, you still get a response from the server with data. See how to get the response in this situation.

I think react-axios should also pass in response when there is an error condition. It doesn’t look like it does currently.

sheaivey commented 7 years ago

I believe error.response will have what you are looking for. I built the error handler the same as the axios core.

https://github.com/mzabriskie/axios/blob/master/README.md#handling-errors

binki commented 7 years ago

Ah. I didn’t make that connection even though I looked at that exact line of code, I can get to the response in my handler after all. Thanks for the help!

sheaivey commented 7 years ago

It might not be a bad idea to change the error handler to:

this.setState({ isLoading: false, response: err.response, error: err })

But there are probably ramifications by not setting response to null if the user does not first check if error is set.

binki commented 7 years ago

Yeah. IMO, it’d be much nicer if response were passed. I basically have to do the following in places where my server returns error information which I want to use/display in the response:

response = response || (error || {}).response || null;

semver says you may change something like that if you bump major. Not sure if people will catch the change in behavior when updating to a new version though. Let me know what you want to do. I think maybe the test case I wrote in #19 could still be useful? ;-)

I guess I’ll reopen this and let you decide what you want to do whenever you find time. No rush ;-).

sheaivey commented 6 years ago

v2.0.0 addresses this change. Now when an error occurs response will contain the err.response object.