【问题标题】:Getting “Uncaught (in promise) Error” from within a catch block从 catch 块中获取“未捕获(承诺中)错误”
【发布时间】:2018-06-22 13:13:56
【问题描述】:

我遇到了一个奇怪的错误,我已经为此苦苦挣扎了将近一天。看来我拒绝的承诺没有被我的catch 块抓住。我对es6 asyncawait 有点陌生;但是,它似乎与我在 C# 中习惯的非常相似。

Get: async function (request, cancelToken = null) {
console.info('Sending GET', request)
store.commit('global/setLoader', true)
let headers = getAuthTokenHeader()
try {
  var res = await Vue.axios({
    url: baseUrl + request.Url,
    params: request.Params,
    method: 'GET',
    headers
  }).catch(function (error) {
    console.error('Failed to GET: ' + error)
    if (typeof error.response !== 'undefined' && typeof error.response.data !== 'undefined' && error.response.data !== null) {
      throw new Error(error.response.data)
    } else {
      throw error
    }
  })
  console.info('Request received', res)
  return res
} finally {
  store.commit('global/setLoader', false)
}
},  

if (typeof error.response !== 'undefined' && typeof error.response.data !== 'undefined' && error.response.data !== null) {线上抛出错误

根据https://javascript.info/async-await#error-handling
我不应该收到这个“Ucaught (in promise)”错误,因为我将整个异步调用包装在 await 中。

调用代码也在等待这个代码。这是参考的呼吁。

static async GetPackagingDimensions (input, Id) {
const request = {
  Url: 'api/packing/getPackagingBoxDimensionsByBarCode',
  Params: {
    boxBarcode: input,
    orderId: Id
  }
}
const res = await HttpUtil.Get(request)
return res

}

有趣的是,如果我将调用代码本身包装在 try catch 中,错误就会被适当地捕获。

为什么我无法从我的捕获中捕获被拒绝的承诺?

快速编辑 - 我的console.log 也没有在 catch 中被调用,但错误似乎是在下面的行中抛出的

【问题讨论】:

  • 因为你的 catch 抛出了一个错误.. ?
  • 但是为什么promise会出错呢?那么不应该有一个简单的旧错误控制台消息吗?
  • 不,因为你已经标记了函数async,它是一个promise,并且永远是一个promise,如果你没有在你的promise中发现一个错误,它就是一个rejected promise,如果没有处理,这是一个未处理的承诺错误。 Shouldn't then there be a plain old error console message不,..
  • 查看您的代码,您的Get 处理程序基本上位于您的承诺链的末尾。当你到达这里时,你必须处理错误,例如在 Node.js 中,如果你不这样做,节点的未来版本甚至会终止进程。我会做的是不要在这里重新抛出,而是用console.error代替。 / 或者如果你有一些好的日志工具,记录它..
  • 另外}).catch(function (error) { 删除它,并使用正确的catch (e) {} 尽量不要将Promise 回调与async / await 混合,这是一种反模式。

标签: javascript ecmascript-6 async-await


【解决方案1】:

try catch 不像 then catch 那样工作。

try {
  var res = await Vue.axios({
    url: baseUrl + request.Url,
    params: request.Params,
    method: 'GET',
    headers
  })
}
catch(error) {
    console.error('Failed to GET: ' + error)
    if (typeof error.response !== 'undefined' && typeof error.response.data !== 'undefined' && error.response.data !== null) {
      throw new Error(error.response.data)
    } else {
      throw error
    }
}

这就是在 js 中使用 try catch 块的方式。 await 解决了承诺

如果在解决 Promise 时发现错误,则将错误抛出到 catch 块

【讨论】:

  • 你好 Prajav,我也试过了,结果相同。
  • 在尝试后使用 catch 在您的代码中毫无意义。捕捉时不涉及任何承诺。顺便说一句,如果您在浏览器上运行此代码,它的兼容性仍然很低。
  • 对不起,我在做的时候打错了。 async / await 不像 then catch 没有任何错误处理,因此 try catch 用于在解决 promise 时检查任何错误。如果存在任何错误,则抛出错误
  • 这更好,因为 OP 重新抛出错误将是有问题的,因为他将它附加到看起来像 get 处理程序的东西上,IOW:承诺将变得未处理,.. 如果你更换用console.error重新抛出,我认为这应该是OP需要的......
  • 您的解决方案是正确的;然而,对于其他人来说,根本问题在于我没有发布的代码。通常,此调用堆栈在多个级别上捕获错误并始终重新抛出。所以无论我在调用堆栈的开头做了什么,总会有一个错误被明确地抛出链上的某个地方。
猜你喜欢
  • 2021-11-28
  • 1970-01-01
  • 2018-07-30
  • 2023-03-27
  • 1970-01-01
  • 2022-12-19
  • 2023-02-06
  • 2019-11-24
  • 2018-05-12
相关资源
最近更新 更多