Middleware
Koa
Koa integration is best handled in the outer middleware layer. That gives errorcore one async boundary around the request and one consistent place to attach request metadata.
app.use(async (ctx, next) => {
return errorcore.withRequestContext(
{
method: ctx.method,
route: ctx.path,
requestId: ctx.get("x-request-id"),
},
async () => {
try {
await next();
} catch (error) {
errorcore.captureError(error);
throw error;
}
},
);
});Keep the wrapper near the top of the stack so downstream middleware inherits the same request scope.