-
-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Axios catch error returns javascript error not server response #960
Comments
I have exactly the same environment. Try this:
Modify from You can also use a global interceptor and reject only the |
In case anybody is wondering how to reject only the
The important part is |
This yields the same result incase if the above solution messes up your JS file syntax while indenting or minifying :)
|
@gopal-g that's undefined for me. |
Well @pedro-mass in that case there might be no response from server side. in my scenario my response was JSON when error occured I could get the response using |
@gopal-g If I'm watching the Network tab in dev tools, I can see the response. It's a 401 if that makes a difference. |
I have the same error with @pedro-mass 's. here is my code async function test () {
try {
let res = await axios.get('/xxxxx/xxxx')
} catch (e) {
console.log(e.response) // undefined
}
} |
using "error.response" does not work, for me. The problem occurs when I disconnected the database server and my web server returned error 500. Network tab in dev toolsresponse code: 500
chrome console error:
version"axios": "^0.15.3" |
There is a catch in |
@alsofronie so what is the preferred method of handling (distinguishing one from another) for example preflight failures like 413? |
I'm getting a similar issue - Chrome devtools is showing a |
@robbiemu I think its already documented: https://github.com/axios/axios#handling-errors |
@mrchief thanks for this, good to go over it again. In my case, this solution has works in most cases but the current problem I am experiencing is that the |
@paolavness I'd suggest to open up a new issue instead of commenting here. Not many watch closed issues. Or maybe even ask it on StackOverflow. |
@mrchief Ah this is closed, thanks for pointing that out. will do. |
I had the same issue. Ultimately, I changed the error code from 401 to 403 and everything worked as I expected it to. I'm guessing the reason for the empty error object and resulting javascript error has to do with this statement that I found in the documentation for 401 errors: "[A 401 error] response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource." So if you are going to utilize a 401 error, you must include a www-authenticate header field. If you don't want to do this, just use a 403 error. |
Good morning guys, I had this issue, however, I fixed it changing my apache configuration to enable cors on my server. Have a look at link below |
You can debug all response only with |
axios.post(url) |
I had exactly the same problem described by @fabiorecife . I solved it but it is not the most elegant solution and I do not understand why this is needed.
This produces the expected results in the |
@petruscurtu I have the same problem! It dose not work for me!
|
@luxueyan I do not know what the problem could be since I do not know how you handle CORS on your server. As far as axios is concerned, I can see a config and request object in your console output. I think that you should check that output. It might be related to the format of the response your server is sending back. |
@luxueyan It seems that you had your |
@petruscurtu The output is 'JSON.parse(JSON.stringify(error))' in interceptor of response. There has not response property |
In my experience: "Modify from console.log(error) to console.log(error.response) in catch." worked better with console.log(error.response.data) in catch. |
axios.interceptors.response.use(null, error => { getClients: async (state) => { |
Another option
|
Try{ } |
|
Great thanks ! Helped a lot! |
still getting this problem. on chrome debugger the response is correct but axios returns a general |
I'm still having this and I am really confused. I tried to catch it is the comments above - but why exactly does axios return undefined when there is an error? **EDIT: found my bug - nothing wrong for me with axios. See my comment two comments down. In short my interceptors were not passing the error data back through the axios pipeline so I was always getting undefined. |
@HoogsterInc Have look at the TypeScript definitions here
|
Thank you for the response trying to help @konstantinblaesi -- I figured out my issue. For me I had an interceptor set up to check the user's authentication, which if they become unauthed with send a specific error code. I was not however passing along the error data when the error did not match an unauthed code... sigh... silly bad mistake. So after checking for the unauthenticated error code I made sure to return Promise.reject(error); - That solved my problem and the error data comes in full now. |
You can use
|
(set for global) in the main.js
or
|
Axios & catch 500 error response dataconst log = console.log;
updateData(options = {}, flag = false){
let url = `/opapi/appDownloadGuide/update`;
let message = '更新成功!';
let errorMessage = '更新失败!';
if (flag) {
message = '添加成功!';
errorMessage = '添加失败!';
url = `/opapi/appDownloadGuide/add`;
}
axios
.post(url, options)
.then(res => {
// ❓🤔️500 , 在这里拦不住呀?
log(`res`, res, res.status);
return res.data;
})
.then(json => {
const {
code,
success,
data,
errorCode,
errorHint,
} = json;
if(code === 200) {
this.$message({
type: 'success',
message,
});
this.init();
} else{
this.$message({
type: 'error',
message: `${errorMessage}: ${errorHint ? errorHint : ""}`,
});
}
})
.catch(err => {
// 👌在这里拦截 error data!
log(`error.response`, err.response);
const {
data,
status,
statusText,
} = err.response;
this.$message({
type: "error",
message: `${statusText}-${status}: ${data || ""}`,
// message: "不能重复创建",
});
console.error(`500 err`, err);
});
}, |
Not working for me |
I'm using Axios 0.19.2. I added @konstantinblaesi 's solution:
This worked for me, thanks so much @konstantinblaesi ! I don't understand why Axios's default behavior isn't like this -- the standard behavior is to drop the "message" part of the original error, which I would think is the most important part! Just to be clear, in my app, my server is returning a 400 error (which should be a response):
and without this fix, I get from Axios an error object that contains only As for why there is no "response" property without this fix, it seems to be related to module.exports = function settle(resolve, reject, response) {
var validateStatus = response.config.validateStatus;
if (!validateStatus || validateStatus(response.status)) {
resolve(response);
} else {
reject(createError(
'Request failed with status code ' + response.status,
response.config,
null,
response.request,
response
));
}
}; and you can see it doesn't pass through |
axios.post('/formulas/create', { |
error.response is an object , it will output [object,object] in console log. |
@chandukasm yes but in my case I do not have an |
if you see [object,object] on console.log then just do JSON.stringify(error.response) to see the actual error. |
There is no return this.$axios
.post(postUrl, payload, { responseType: 'json' })
.then(
(response) => {
this.message = `RESP: ${JSON.stringify(response)}`;
},
(reason) => {
this.message = `REAS: ${JSON.stringify(reason)}`;
}
)
.catch((err) => {
this.message = `CATCH: ${JSON.stringify(err)}`;
}); payload contains a bad value on purpose to trigger HTTP 422, which in Postman works as expected and the server (LB4) gives back complete error in JSON format explaining where the input data was wrong, but in axios, the error.response object is undefined. |
Im trying to catch validation errors from the server.
Code:
Console log output
Network tab output
{"name":["The name field is required."],"parts":["The parts field is required."]}
I should be seeing an object that contains JSON form validation as that is the response in my network tab, i seem to get the JS catch output?
Also tried the following:
Same result
More people seem to have the same problem using the same enviroment as me here.
https://laracasts.com/discuss/channels/vue/issues-with-axios-catch-method
The text was updated successfully, but these errors were encountered: