0

我在 catch 中收到以下错误:

Blockquote UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .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 调用显示:{status: 'ERROR', code: 'EMAIL_FAIL', message: 'Email failed 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');
    });
  });

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

4

1 回答 1

0

您没有从 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');
    }
  });

于 2020-08-10T16:43:05.247 回答