捕获中未处理的承诺拒绝

Unhandled promise rejection inside a catch

我在捕获中遇到以下错误:

Blockquote UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originared either by throwing inside of an async function without a catch block, orby rejecting a promise which was not handled with .catch().

这是调用它的代码:

http({
      method: 'post',
      url: config.uri,
      headers: headers,
      data: data
    }).then(function (response) {
      resolve(response.data);
    }).catch(function (error) {
      console.log(error.response.data);
      reject(error.response.data);
    });

catch 中的 console.log 调用读取:{状态:'ERROR',代码:'EMAIL_FAIL',消息:'Email fail to send' }

调用函数的代码:

router.post('/declaration', csrf, async function (req, res, next) {
    let reqId = generateReqId();
    const ref = reqId.split('-')[0];
   let data = buildSubmission(fakeSub, res.locals.locale.toUpperCase(), ref);
    let headers = { ref: data.ref, reqId: reqId };
    const response = await callAPI.submitApplication(data, headers);

    casaApp.endSession(req).then(() => {
      res.status(302).render('../views/pages/confirmation.njk');
    }).catch((err) => {
      console.log(err);
      res.status(302).render('../views/pages/exit-error.njk');
    });
  });

为什么我会收到这个? 我希望代码 return 到将处理问题的调用段。 有什么建议吗?

您没有捕获来自 callAPI.submitApplication 请求的错误:

router.post('/declaration', csrf, async function (req, res, next) {
    const reqId = generateReqId();
    const ref = reqId.split('-')[0];
    const data = buildSubmission(fakeSub, res.locals.locale.toUpperCase(), ref);
    const headers = { ref: data.ref, reqId: reqId };
    try {
      const response = await callAPI.submitApplication(data, headers);
      await casaApp.endSession(req);
      res.status(302).render('../views/pages/confirmation.njk');
    } catch(e) {
      console.log(err);
      res.status(302).render('../views/pages/exit-error.njk');
    }
  });