How can I get response body for 404 in Axios? - javascript

I'm calling an API that returns 404 and a message in the body.
I want to get that message in Axios.
I saw Axios. How to get error response even when api return 404 error, in try catch finally but it does not work.
I also tried error.response.response but it's undefined.
How can I get the textual response from 404?

try the example set in https://axios-http.com/docs/handling_errors
in particular: error.message or error.response

Related

Can't log the response however I can see it in Postman and network tab

I am doing register with an endpoint for it. The problem now is handling validation when it comes from backend. That means when I send a username less than 6 letters backend respondes to that saying username should be more than 6 letters.
Now, the response from backend shows inside network tab, and shows in postman but when I do console.log(response) it shows an error saying referenceError: response is not defined.
My code
axios.post(url, payload,{
})
.then( response => {
console.log('response')
console.log(response)
} )
According the axios docs when response status is out of 2xx you must use catch block. see
The then block is not executing because the 4xx status codes are considered errors. Axios then throws an exception which you need to catch. To handle the 4xx cases you can use a catch block, which will execute whenever there is an error with your Axios request.

No response in browser but getting 401 in postman

I'm calling an API with a JWT. When the token expires or when I send an invalid token, I get a 401 in postman, but when I do it in browser, I get a "Network error". And it works when the token is valid.
I use axios to call the API and the error.response is undefined. When I stringify the error object in catch I see something like this:
{
message : 'Network Error',
name : 'Error',
stack : 'Error: Network Error\n at createError (webpack-internal:///./node_modules/axios/lib/core/createError.js:16:15)\n at XMLHttpRequest.handleError (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:83:14)'
}
I get 200 in the OPTIONS request, but in the main GET request I get the above error. This is probably because of CORS. But I don't know what to change. The Access-Control-Allow-Origins is *.
I don't understand why I don't get the 401 in browser. Can someone please tell me how to fix this?
Screenshot of network tab:
Options:
Your server is probably not setting Access-Control-Allow-Credentials to true to allow origin access to authorized users.
I don't think you actually have a bug. It's just that axios throws on all 4xx responses by default. Try the following:
axios.get("...", { validateStatus(status) { return true; } })
To fix it up, try add other Cross-Origin Resource Sharing (CORS) headers. Check them at
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Axios catch error returns JavaScript error not server response

Im trying to catch validation errors from the server.
Code:
axios.post('https://myBackendIp.com/api/teacher/time/create',Data)
.then(res=>{
console.log(res)
}).catch(error=>{
console.log(error)
})
Console log output
Error: Network Error
at createError (createError.js:16)
at handleError (xhr.js:87)
Network tab output
{"error":{"message":"Check your date field","status_code":500}}
Is there a way to change that or I'm doing something wrong?
Thanks!
After Googling a little, I found this ticket:
https://github.com/axios/axios/issues/960
It recommends using:
console.log(error.response)
instead of
console.log(error)
Edit 1:
Did some further reading, apparently this could be a symptom of a CORS issue, see this and this.
The request on the network tab might seem like it's succeeding, however, when it's processed by Axios, a Network Error is returned.

express return status code for invalid operation

I use redux promise middleware for my front end. so action.type will catch if there any error, but what's the correct status code for error I should throw from my backend?
I know I can do res.status(500).json(something)
but is 500 correct code for error? like invalid password, should I throw 500 too?
You can check out the error code in wiki as well as , they have mention all the error status code you should use.
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Most if you have error related to the data in the api , password wrong they come under 4XX error.
Like Password error you should give 401 Unauthorized

In what way Backbone.js error function is triggered

In Backbone.js in what terms the error state is triggered. I'm facing a challange and error is triggered even it receives 200. I've read it an article that success can be forced by sending a 204 status code from php.
What are the circumstances that causes error to trigger?
If your response is returning anything other than JSON, even with a 200 status, you'll get an error. For instance, in the app I'm building, I'm calling into an old REST endpoint that returns XML. The response is valid, but error gets invoked every time. If you can get your response to return as JSON, the error condition should go away.

Categories

Resources