---
title: "Fastify Error Handler"
description: "Learn about Sentry's Fastify SDK Error Handler and how to configure it."
url: https://docs.sentry.io/platforms/javascript/guides/fastify/features/error-handler/
---

# Fastify Error Handler | Sentry for Fastify

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.

## [Configuration](https://docs.sentry.io/platforms/javascript/guides/fastify/features/error-handler.md#configuration)

You can configure the error handler using the `setupFastifyErrorHandler` function:

```javascript
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);
```

## [Options](https://docs.sentry.io/platforms/javascript/guides/fastify/features/error-handler.md#options)

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.

```typescript
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.

```typescript
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;
  },
});
```
