I'm building an API with Koa. I have all my routes in place with koa-router. Each route uses a controller that has all the logic for a given mongoose model. I've read the Koa docs on error-handling and understand the use of await
ing in a try/catch
block. There they mention a Default Error Handler should be set at the beginning of the middleware chain. So if I was to have something like the following, I should have resonable error handling for the route at router.get()
:
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
err.status = err.statusCode || err.status || 500;
throw err;
}
});
router
.get('/', async (ctx, next) => {
console.log('Got Route');
//ctx.body = users;
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000, () => console.log('Koa app listening on 3000'));
If I was to have something slightly more complex at this route, is there any benefit of adding another try/catch
inside the route?
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) {
console.log(e);
}
await next();
});
Am I just adding redundant error handling here?