Koa@2 error handling
I re-read the docs on Error Handling and also found this little tip on the Koa Wiki. From that, I've concluded the following:
Koa Error Handling states:
However, the default error handler is good enough for most use cases.
The default error handler in this case is the Koa-built-in error handler. You do not need to include any kind of custom error handling in the code you write. Koa will write out a stack trace along with the error message, etc.
If you want to modify how the error is handled, add something like the suggested middleware at the very beginning of the middleware chain:
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
err.status = err.statusCode || err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
}
});
If you want to change that again for a specific route, or a special bit of logic, then add another try/catch
block at that point (as per my above):
router
.put('/', async function updateOnServer(ctx, next) {
try {
await Model.updateOne({
_id: ctx.params.id,
}, {
field1: ctx.request.body.field1,
$push: { field2: ctx.request.body.field2 },
}).exec();
} catch (e) {
ctx.status = 418;
ctx.body = "a custom error message, with nothing really helpful";
}
await next();
});