我在陷阱中遇到以下错误:
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);
});
捕获中的console.log调用显示为:{状态:'ERROR',代码:'EMAIL_FAIL',消息:'电子邮件发送失败'
此代码调用该函数:
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');
});
});
我为什么要得到这个? 我希望代码返回到将处理该问题的调用段。 有什么建议吗?
答案 0 :(得分: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');
}
});