简体   繁体   English

使用 axios 时,catch 块中的状态代码 300 返回错误

[英]Status code 300 return error in catch block when using axios

当我使用 axios 进行 http 调用时,它会进入 catch 块并出现错误

I think you have multiple choices.我认为你有多种选择。 You can either extract your code from then callback into function - and call it in error handler when status code is 300.您可以提取您的代码then回调到功能-当状态代码为300叫它在错误处理程序。

You could also try this option from axios, to reject the promise only when the status code is > 301.您也可以从 axios 尝试此选项,仅当状态代码 > 301 时才拒绝承诺。

axios.get('/user/12345', {
  validateStatus: function (status) {
    return status <= 300; // Reject only if the status code is greater than 300
  }
})

I've needed something a bit more dynamic:我需要一些更有活力的东西:

// oversimplified index.js

const axios = require('axios')

const allowedStatusList = json.parse(process.env.ALLOWED_STATUSES) || ["2xx"]
// ^^ check that it's a valid input yourself :p 

const validateRequestStatusCode = allowedStatuses => {
  const stringStatuses = allowedStatuses.map(String)
  return status => {
    if (stringStatuses.includes(String(status))) {
      return true
    } else {
      for (let wildCardStatus of stringStatuses) {
        const validRange = wildCardStatus.match(/.+?(?=xx)/g)
        if (validRange && String(status).substring(0, 1) == validRange[0]) {
          return true
        }
      }
    }
    return false
  }
}

const axiosConfig = {
  validateStatus: validateRequestStatusCode(allowedStatusList),
  url,
  data
  // etc
}

axios(axiosConfig)
.then(...)
.catch(...)

This would allow you to just supply an environment variable (or however you want to do your config) to change what statuses are valid.这将允许您只提供一个环境变量(或者您想要进行的配置)来更改哪些状态是有效的。

For example, setting ALLOWED_STATUSES='["2xx", "3xx", "4xx"]' would allow any status code from 200 and 499. Set ALLOWED_STATUSES='[200, 201, "3xx"]' and you'd have a far more specific filter.例如,设置ALLOWED_STATUSES='["2xx", "3xx", "4xx"]'将允许来自 200 和 499 的任何状态代码。设置ALLOWED_STATUSES='[200, 201, "3xx"]'并且你有一个更具体的过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查 Axios promise 捕获块 VueJs 中的 HTTP 状态代码 - Check HTTP status code in the Axios promise catch block VueJs 处理状态码错误 - 尝试在死连接上捕获 Axios - Handling status code error - try catch Axios on dead connection 等待 axios 后调用后的代码未运行,catch 块中没有错误 - code after await axios post call is not running, no error in catch block axios如何在.catch()中获取状态代码? - How can axios get the status code in .catch()? 如何使用Axios捕获服务器代码错误 - How do I catch a server code error using Axios Axios 错误状态码 UnhandledPromiseRejectionWarning - Axios error status code UnhandledPromiseRejectionWarning 当 try...catch 块之一抛出错误时,从 function 返回 - Return from a function when one of try ... catch block throws an error 使用 Get 方法时出现状态码为 415 的 Axios 错误 - Axios Error with Status Code 415 in Using Get Method 使用异步等待获取失败时如何捕获状态代码 - How to catch the status code when fetch fails using async await 爱讯。 即使api返回404错误,如何在try catch finally中获得错误响应 - Axios. How to get error response even when api return 404 error, in try catch finally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM