【问题标题】:How to handle API request that does not throw catch? (403 error)如何处理不抛出 catch 的 API 请求? (403 错误)
【发布时间】:2021-12-06 23:29:56
【问题描述】:

尝试使用以下方法访问 blob 存储 API,但得到 403 返回而没有错误抛出(发生这种情况是因为我试图访问不存在/没有权限的文件)。这种错误应该如何处理? .catch 已在调用 API 时应用。

背景.. 我试图故意犯错误,以便在 Azure blob 存储中获取文件。我确实期待 403,我希望妥善处理。

获取方法

 getReq = async url => {
    return new Promise((resolve, reject) => {
      axios
        .get(url)
        .then(function (response) {
          resolve(response.data);
        })
        .catch(function (error) {
          reject(error);
        })
    });
  };

收到邮递员的回复

<?xml version="1.0" encoding="utf-8"?>
<Error>
    <Code>AuthenticationFailed</Code>
    <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:maskedInfo
Time:maskedInfo</Message>
    <AuthenticationErrorDetail>Signature did not match. String to sign used was 


/blob/maskedInfo/maskedInfo/maskedInfo.json
ReadOnly


2020-08-04
b





</AuthenticationErrorDetail>
</Error>

试过了:

  1. TryCatch 也没有发现问题

来自 React Native 的错误显示:

【问题讨论】:

  • 如何调用getReq()方法?
  • @BadPiggie 惯用的async await 风格,不知道你想理解什么。
  • 显示代码如何调用方法getReq()

标签: javascript react-native azure-blob-storage


【解决方案1】:

当你调用getReq()方法时,它会返回一个Promise。所以,你应该处理拒绝。

getReq("https://somerandomurl.com")
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.error(err);
  })

在等待风格中,

try {
  const res = await getReq("https://somerandomurl.com");
} catch (err) {
  console.error(err);
}

【讨论】:

    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2020-10-28
    • 2019-07-23
    • 2021-08-07
    相关资源
    最近更新 更多