【问题标题】:What is the shape of error object inside axios request interceptor error handler?axios 请求拦截器错误处理程序中错误对象的形状是什么?
【发布时间】:2019-06-29 11:06:48
【问题描述】:

技术说明: 由于 axios 对 Node 和浏览器使用不同的库/机制,这个问题只涉及 Node.jsaxios@0.18.0 的使用。

我可以为axios 库(https://github.com/axios/axios#interceptors)设置以下拦截器:

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    //
    // I am asking about this error handler and this error object
    //
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

何时触发请求拦截器的错误处理程序中描述的回调,该错误对象的形状是什么?

P.S.我看到axios中有this code describing work with errors

axios.get('/user/12345')
  .catch(function (error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      //
      //
      //  !!!! This is a request error handler !!!
      //
      //
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  });

请求错误处理程序中的error在后面的代码中代表什么?

【问题讨论】:

    标签: javascript node.js typescript axios


    【解决方案1】:

    当请求拦截器的错误处理程序中描述的回调 被触发了,那个错误对象的形状是什么?

    错误处理程序(.catch 子句)将由拦截器触发,当它“拒绝”你代码中这部分的承诺时:

    axios.interceptors.response.use(function (response) {
        // Do something with response data
        return response;
      }, function (error) {
        // Do something with response error
        return Promise.reject(error); // <---- HERE
      });
    

    axios 错误对象的形状是一个 JSON 对象,如handling error section of axios docs on github 所述

    • message:错误消息文本。
    • response:如上一节所述的响应对象(如果收到)。在响应中,您将拥有数据、状态和标头对象
    • request:在浏览器上运行时的实际 XMLHttpRequest 对象或 node.js 中 http.ClientRequest 的实例。
    • config:原始请求配置。

    请求错误处理程序中的错误将在 后面的代码?

    这将是你的 axios 拦截器绕过的请求的错误响应

    【讨论】:

      【解决方案2】:

      我认为这个源代码可能会对你有所帮助:

      createError.js

      它看起来像Error 的一个实例,因此它具有error.message,并且axios 添加了error.configerror.codeerror.requesterror.response,每个enhanceError.js

      【讨论】:

        猜你喜欢
        • 2019-04-06
        • 2022-11-20
        • 2020-11-28
        • 1970-01-01
        • 1970-01-01
        • 2018-04-25
        • 2020-12-14
        • 2018-06-19
        • 1970-01-01
        相关资源
        最近更新 更多