-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
+72
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
docs/platforms/javascript/guides/fastify/features/error-handler.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
## 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; | ||
}, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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