如何从Axios中的http错误中获取状态代码?

时间:2022-03-01 01:52:25

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

这可能看起来很愚蠢,但是当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'}

4 个解决方案

#1


93  

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

你看到的是错误对象的toString方法返回的字符串。 (错误不是字符串。)

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);
    }
  });

#2


9  

As @Nick said, the results you see when you console.log a JavaScript Error object depend on the exact implementation of console.log, which varies and (imo) makes checking errors incredibly annoying.

正如@Nick所说,你在console.log中看到的结果是一个JavaScript Error对象取决于console.log的确切实现,它随之变化而且(imo)使得检查错误非常烦人。

If you'd like to see the full Error object and all the information it carries bypassing the toString() method, you could just use JSON.stringify:

如果您希望看到完整的Error对象及其绕过toString()方法的所有信息,您可以使用JSON.stringify:

axios.get('/foo')
  .catch(function (error) {
    console.log(JSON.stringify(error))
  });

#3


4  

I am using this interceptors to get the error response.

我正在使用此拦截器来获取错误响应。

const HttpClient = axios.create({
  baseURL: env.baseUrl,
});

HttpClient.interceptors.response.use((response) => {
  return response;
}, (error) => {
  return Promise.resolve({ error });
});

#4


1  

This is a known bug, try to use "axios": "0.13.1"

这是一个已知的bug,尝试使用“axios”:“0.13.1”

https://github.com/mzabriskie/axios/issues/378

https://github.com/mzabriskie/axios/issues/378

I had the same problem so I ended up using "axios": "0.12.0". It works fine for me.

我有同样的问题所以我最终使用“axios”:“0.12.0”。这对我来说可以。

#1


93  

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

你看到的是错误对象的toString方法返回的字符串。 (错误不是字符串。)

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);
    }
  });

#2


9  

As @Nick said, the results you see when you console.log a JavaScript Error object depend on the exact implementation of console.log, which varies and (imo) makes checking errors incredibly annoying.

正如@Nick所说,你在console.log中看到的结果是一个JavaScript Error对象取决于console.log的确切实现,它随之变化而且(imo)使得检查错误非常烦人。

If you'd like to see the full Error object and all the information it carries bypassing the toString() method, you could just use JSON.stringify:

如果您希望看到完整的Error对象及其绕过toString()方法的所有信息,您可以使用JSON.stringify:

axios.get('/foo')
  .catch(function (error) {
    console.log(JSON.stringify(error))
  });

#3


4  

I am using this interceptors to get the error response.

我正在使用此拦截器来获取错误响应。

const HttpClient = axios.create({
  baseURL: env.baseUrl,
});

HttpClient.interceptors.response.use((response) => {
  return response;
}, (error) => {
  return Promise.resolve({ error });
});

#4


1  

This is a known bug, try to use "axios": "0.13.1"

这是一个已知的bug,尝试使用“axios”:“0.13.1”

https://github.com/mzabriskie/axios/issues/378

https://github.com/mzabriskie/axios/issues/378

I had the same problem so I ended up using "axios": "0.12.0". It works fine for me.

我有同样的问题所以我最终使用“axios”:“0.12.0”。这对我来说可以。