服务中 Asynctask 的错误未传递到控制器并作为 Api 响应传递

编程入门 行业动态 更新时间:2024-10-03 17:16:11

服务中 Asynctask 的错误未传递到<a href=https://www.elefans.com/category/jswz/34/1771349.html style=控制器并作为 Api 响应传递"/>

服务中 Asynctask 的错误未传递到控制器并作为 Api 响应传递

我有一个nodejs API 控制器,它路由到我的nodejs 服务器服务中的逻辑。控制器代码如下所示:

  router.post('/processPayment', (req, res, next) => {
  const payload = jwtDecode(req.headers.authorization.split(' ')[1]);
  authService.getAuthToken(payload.sub)
    .then(token => cloverPaymentService.processPayment(req.body, token.merchantId,token.cloverToken))
    .then(token => cloverPaymentService.processEncryptionV2(token.response, token.data, token.merchantId, token.cloverToken))
    .then(token => cloverPaymentService.processPaymentV2(token.response, token.data, token.merchantId, token.cloverToken))
    .then(paymentResponse => res.send(paymentResponse))
    .catch(err => next(err));
});

在我的服务中,我有以下代码可以发送发布请求并捕获任何错误:

const response = await got(options).catch(err=>{
    const parsedError = new HTTPError(err)
    console.error('error ' +JSON.stringify(parsedError.response.body));
    throw new Error('error: ' +parsedError.message);
  });

错误在这里被捕获并在控制台中打印为:

"{\"message\":\"400 Bad Request\",\"error\":{\"type\":\"invalid_request_error\",\"code\":\"email_invalid\",\"message\":\"Please provide a valid email.\"}}"

即使错误被记录并显示在服务器控制台中,错误也不会传递到控制器并传递回 api 调用。相反,我只是得到一个标准的 500 内部错误:

Error: error: Response code undefined (undefined)
    at /Payment.service.js:221:11
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async Object.processPaymentV2 (Payment.service.js:218:20)

我应该从运行此 api 调用中获取 400 错误代码,而不是 500 内部错误,并且在这方面遇到了一些问题。有谁知道如何修复或改进错误处理?

回答如下:

更多推荐

服务中 Asynctask 的错误未传递到控制器并作为 Api 响应传递

本文发布于:2024-05-31 05:57:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   错误   Asynctask   Api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!