Comments (17)
This has been added as a feature in 0.11.0
by use of validateStatus
config option.
from axios.
@carlojacobs you can access error.response.data
axios.post('/').then(response => {
console.log(response.data);
}).catch(error => {
console.log(error.response.data);
});
from axios.
The problem is with response interceptor. It puts error object in then
callback instead catch
:
axios.interceptors.response.use(response => {
return response;
}, error => {
return error;
});
from axios.
I can't think of a compelling reason to have axios to take server errors and throw them as runtime errors. Much preferred to handle the response codes manually, especially since TypeScript doesn't provide types for thrown errors that I'm aware of, so you end up having to go digging through an untyped object to try to figure out what the heck is going on. It also breaks up app flow considerably. Just one mans opinion.
from axios.
@mbektimirov After seeing @clayreimann suggestion I have changed my interceptor as below.
Now error resolves in catch block rather than then block.
Hope this helps someone.
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
return Promise.reject(error);
});
from axios.
It's hard to make a change like this at this point. Where people expect it to catch on anything outside of the 2xx range. In retrospect, if the request was made, and a response received I would have preferred it to be considered a success. This would be a much less opinionated implementation, and much better IMO. I think the way to handle this now would be to pass a config option that specifies what range to catch on, defaulting with anything outside of 2xx.
from axios.
Same thing for me, 400 (BAD REQUEST)
error resolves in then
, not in catch
. Very annoying bug, breaks all REST flow.
from axios.
@mbektimirov A little late here, but I'm guessing that you're seeing the error
in then
because your error callback isn't throw
ing the error.
from axios.
I totally y agree, it shouldn't throw any error passed 500 errors, which basically prevents doing any REST... I think this is very "bad design" (don't be mistaken, the library is great!!)
How is this resolved pass 0.11.0, any example on how to use the option maybe ? (using 0.16 here)
Is there any way to use this in a classic GET/POST shortcut?
Thanks!
from axios.
(on 0.16.2) - 400 Response still resolves in then instead of catch, and response object is undefined regardless of what server returns.
from axios.
I'm confused. Does axios fail the promise on 4xx? This thread makes it sound like it DOES, but in my code i'm getting a resovled promise on status 4xx. It doesn't appear to be documented.
from axios.
The code says that it's resolved when request.status >= 200 && request.status < 300
and rejected in any other case.
https://github.com/mzabriskie/axios/blob/1629a026da17a1e1d8999a02f3fe6b6b60aaac9c/lib/adapters/xhr.js#L58
https://github.com/mzabriskie/axios/blob/db85c7bf3ae19d680f5c16cd85d06c5e11fedc5f/lib/adapters/http.js#L80
from axios.
+1
Using axios for the first time. Definitely like it, but did just run into this; it would be great for the 400s to not throw errors.
Yay! Assigned and in progress as of 4 days ago. Thanks y'all!
from axios.
axios.defaults.validateStatus = function () {
return true;
};
Or
axios({
...,
validateStatus: function () {
return true;
}
});
from axios.
@mzabriskie +1
from axios.
Agreed, luckily the response is in the error object so you can deal with it, but would make sense to have it in the regular flow. I always felt like 200-400s was more related to http, whereas 500+ were more application errors. E.g. validaation failed, etc.
from axios.
I agree, I use 401 response in case of a login failure, in that case I'm also sending a message, but because it passes it to the .catch(), I can't access that data.
Any news on how to fix/modify this?
from axios.
Related Issues (20)
- test cases fails when the interceptor header exceeds 10 characters
- Form data key of "x-amz-security-token" changes from string key to object {x: { amz: {security: { token: <value> } } } }
- What is the correct version guide of Axios? HOT 2
- SSRF Vulnerability Arising from Axios URL Parsing HOT 3
- Why is the headers attribute of InternalAxiosRequestConfig mandatory?
- Security Vulnerability from follow-redirects HOT 1
- axios V0.28.0 Uncaught ReferenceError: req is not defined at AbortSignal.onCanceled HOT 7
- Network Error in React Native HOT 5
- Axios lib gets detected as a trojan HOT 3
- Can't find variable: axios error thrown on older browser (since v.1.6.8) HOT 1
- Invalid package attestation - 1.6.8 HOT 4
- Axios calculating wrong Content-Length HOT 2
- Information Exposure due to the handling of the Proxy-Authorization header across hosts HOT 1
- The interface has a return value, but the axios report response is undefined
- http req with proxy + ca doesn't work HOT 1
- Axios 500 Error Problem HOT 3
- Security: Axios sends HTTPS data in cleartext to a proxy (regression)
- Proxy environment variables ignored HOT 1
- Axios Unable to Handle "204 No Content" Responses
- Request body converted the params into string type causing wrong response as compare to network request using fetch (React native) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axios.