Skip to content

Commit 173f730

Browse files
authored
feat(fastify): Document shouldHandleError (#13111)
1 parent 8361005 commit 173f730

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Fastify Error Handler
3+
description: "Learn about Sentry's Fastify SDK Error Handler and how to configure it."
4+
---
5+
6+
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.
7+
8+
## Configuration
9+
10+
You can configure the error handler using the `setupFastifyErrorHandler` function:
11+
12+
```javascript
13+
import * as Sentry from "@sentry/node";
14+
import { setupFastifyErrorHandler } from "@sentry/fastify";
15+
16+
const app = fastify();
17+
18+
// Initialize Sentry
19+
Sentry.init({
20+
dsn: "your-dsn",
21+
});
22+
23+
// Setup the error handler
24+
setupFastifyErrorHandler(app);
25+
```
26+
27+
## Options
28+
29+
The `setupFastifyErrorHandler` function accepts an optional options object that can be used to customize the error handler.
30+
31+
- `shouldHandleError` _version 9.9.0+_
32+
33+
A function that determines whether an error should be captured.
34+
35+
```typescript
36+
declare function shouldHandleError(
37+
error: Error,
38+
request: FastifyRequest,
39+
reply: FastifyReply
40+
): boolean;
41+
```
42+
43+
```javascript
44+
setupFastifyErrorHandler(app, {
45+
shouldHandleError(error, request, reply) {
46+
return reply.statusCode >= 500 || reply.statusCode <= 399;
47+
},
48+
});
49+
```
50+
51+
If using TypeScript, you can cast the request and reply to get full type safety.
52+
53+
```typescript
54+
import { FastifyRequest, FastifyReply } from "fastify";
55+
56+
setupFastifyErrorHandler(app, {
57+
shouldHandleError(error, minimalRequest, minimalReply) {
58+
const request = minimalRequest as FastifyRequest;
59+
const reply = minimalReply as FastifyReply;
60+
return reply.statusCode >= 500 || reply.statusCode <= 399;
61+
},
62+
});
63+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Fastify Features
3+
description: "Learn how Sentry's Node SDK exposes features for first class integration with Fastify."
4+
excerpt: ""
5+
---
6+
7+
The Sentry Node SDK offers Fastify-specific features for first class integration with the framework.
8+
9+
<PageGrid />

0 commit comments

Comments
 (0)