Fastify Error Handler
Learn about Sentry's Fastify SDK Error Handler and how to configure it.
The Fastify error handler integration automatically captures errors in your Fastify application and sends them to Sentry. By default, it captures all errors with status codes 5xx and above, as well as errors with status codes 2xx and below.
You can configure the error handler using the setupFastifyErrorHandler
function:
Copied
import * as Sentry from "@sentry/node";
import { setupFastifyErrorHandler } from "@sentry/fastify";
const app = fastify();
// Initialize Sentry
Sentry.init({
dsn: "your-dsn",
});
// Setup the error handler
setupFastifyErrorHandler(app);
The setupFastifyErrorHandler
function accepts an optional options object that can be used to customize the error handler.
shouldHandleError
version 9.9.0+
A function that determines whether an error should be captured.
Copied
declare function shouldHandleError(
error: Error,
request: FastifyRequest,
reply: FastifyReply,
): boolean;
If using TypeScript, you can cast the request and reply to get full type safety.
Copied
import { FastifyRequest, FastifyReply } from "fastify";
setupFastifyErrorHandler(app, {
shouldHandleError(error, minimalRequest, minimalReply) {
const request = minimalRequest as FastifyRequest;
const reply = minimalReply as FastifyReply;
return reply.statusCode >= 500 || reply.statusCode <= 399;
},
});
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").