For those who are looking for a simple solution, just copy and paste this in bootstrap.js:
strapi.app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
if (err instanceof SomeCustomError) {
return ctx.send(err.body, err.status);
}
throw err;
}
});
strapi.app being the koa instance if I am correct.