errorcore
Middleware

Fastify

Fastify already has a well-defined request lifecycle. Add request context in a hook that runs before handlers, then capture in the shared error handler.

fastify hooks
app.addHook("onRequest", async (request) => {
  errorcore.setRequestContext({
    method: request.method,
    route: request.routerPath ?? request.url,
    requestId: request.id,
  });
});

app.setErrorHandler((error, request, reply) => {
  errorcore.captureError(error, { request: { method: request.method, route: request.url } });
  reply.send(error);
});

Avoid capturing both in a hook and in setErrorHandler, or the same request can produce duplicate incidents.

On this page