Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fastify): Document shouldHandleError #13111

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: Fastify Error Handler
description: "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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is it catching 200 as well?

Suggested change
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.
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 below 2xx.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's catching 2xx as well


## 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

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

```javascript
setupFastifyErrorHandler(app, {
shouldHandleError(error, request, reply) {
return reply.statusCode >= 500 || reply.statusCode <= 399;
},
});
```

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;
},
});
```
9 changes: 9 additions & 0 deletions docs/platforms/javascript/guides/fastify/features/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Fastify Features
description: "Learn how Sentry's Node SDK exposes features for first class integration with Fastify."
excerpt: ""
---

The Sentry Node SDK offers Fastify-specific features for first class integration with the framework.

<PageGrid />