Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the status code from an HTTP error in Axios?

This may seem stupid, but I'm trying to get the error data when a request fails in Axios.

axios   .get('foo.com')   .then((response) => {})   .catch((error) => {     console.log(error); //Logs a string: Error: Request failed with status code 404   }); 

Instead of the string, is it possible to get an object with perhaps the status code and content? For example:

Object = {status: 404, reason: 'Not found', body: '404 Not found'} 
like image 227
Sebastian Olsen Avatar asked Aug 25 '16 19:08

Sebastian Olsen


People also ask

Does Axios throw error on 404?

By default, axios throws an error when it encounters any 4XX / 5XX status code. Adding this line overrides that default behavior. You can then check the status code yourself (in response. status ), conditionally handling the 404 as you need.

Does Axios catch 400 error?

By default, the axios HTTP library throws an error anytime the destination server responds with a 4XX / 5XX error (for example, a 400 Bad Request ). Since axios raises an error, your workflow will stop at this step.

Does Axios throw error on non 200?

It's not possible to retrieve response bodies for non 200 HTTP responses because Axios throws an exception for non 2xx codes. This does not complies to the browser Fetch API. Some APIs return data even if the response code is not 200 OK.


2 Answers

What you see is the string returned by the toString method of the error object. (error is not a string.)

If a response has been received from the server, the error object will contain the response property:

axios.get('/foo')   .catch(function (error) {     if (error.response) {       console.log(error.response.data);       console.log(error.response.status);       console.log(error.response.headers);     }   }); 
like image 186
Nick Uraltsev Avatar answered Sep 28 '22 08:09

Nick Uraltsev


With TypeScript, it is easy to find what you want with the right type.

This makes everything easier because you can get all the properties of the type with autocomplete, so you can know the proper structure of your response and error.

import { AxiosResponse, AxiosError } from 'axios'  axios.get('foo.com')   .then((response: AxiosResponse) => {     // Handle response   })   .catch((reason: AxiosError) => {     if (reason.response!.status === 400) {       // Handle 400     } else {       // Handle else     }     console.log(reason.message)   }) 

Also, you can pass a parameter to both types to tell what are you expecting inside response.data like so:

import { AxiosResponse, AxiosError } from 'axios' axios.get('foo.com')   .then((response: AxiosResponse<{user:{name:string}}>) => {     // Handle response   })   .catch((reason: AxiosError<{additionalInfo:string}>) => {     if (reason.response!.status === 400) {       // Handle 400     } else {       // Handle else     }     console.log(reason.message)   }) 
like image 21
Yan QiDong Avatar answered Sep 28 '22 09:09

Yan QiDong