Error handling 膝关节炎错误处理:是否需要返回?
我正在使用koa路由器处理向第三方api发送电子邮件请求的路由。我的错误处理正确吗?我需要退货吗?我是否应该返回Error handling 膝关节炎错误处理:是否需要返回?,error-handling,koa,Error Handling,Koa,我正在使用koa路由器处理向第三方api发送电子邮件请求的路由。我的错误处理正确吗?我需要退货吗?我是否应该返回ctx.response?我看到一些以wait next()结束func的示例。然而,我假设我不需要它,因为没有其他的func/中间件可以下载到下游 router.post('sendemail', async (ctx) => { const emailData = ctx.request.body; try { await someEmailApi({
ctx.response
?我看到一些以wait next()结束func的示例。然而,我假设我不需要它,因为没有其他的func/中间件可以下载到下游
router.post('sendemail', async (ctx) => {
const emailData = ctx.request.body;
try {
await someEmailApi({
recipient: {
name: emailData.recipientName,
address: emailData.recipientEmail,
},
sender: {
name: emailData.senderName,
address: emailData.senderEmail,
},
subject: mail.subject,
message: mail.message,
});
ctx.response.status = 200;
ctx.response.body = 'OK';
} catch (err) {
ctx.response.status = err.status;
ctx.response.body = err.message';
ctx.throw(ctx.response.status, ctx.response.body);
}
});
因为这是一个路由处理程序,所以通常不调用
wait-next()
,因为路由处理程序是“最内部”的中间件,所以next()
是不可操作的
如果使用的是ctx.throw
,则无需分别设置状态和正文
这就足够了:
ctx.throw(err.status, err.message)
因为这是一个路由处理程序,所以通常不调用
wait-next()
,因为路由处理程序是“最内部”的中间件,所以next()
是不可操作的
如果使用的是ctx.throw
,则无需分别设置状态和正文
这就足够了:
ctx.throw(err.status, err.message)