From 4f265707bac273d7e5fc43b2bf8b21dbcef739ab Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 24 Mar 2025 14:58:32 -0400 Subject: [PATCH] feat(fastify): Document shouldHandleError --- .../guides/fastify/features/error-handler.mdx | 63 +++++++++++++++++++ .../guides/fastify/features/index.mdx | 9 +++ 2 files changed, 72 insertions(+) create mode 100644 docs/platforms/javascript/guides/fastify/features/error-handler.mdx create mode 100644 docs/platforms/javascript/guides/fastify/features/index.mdx diff --git a/docs/platforms/javascript/guides/fastify/features/error-handler.mdx b/docs/platforms/javascript/guides/fastify/features/error-handler.mdx new file mode 100644 index 0000000000000..af18d44139e9d --- /dev/null +++ b/docs/platforms/javascript/guides/fastify/features/error-handler.mdx @@ -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; + }, +}); +``` diff --git a/docs/platforms/javascript/guides/fastify/features/index.mdx b/docs/platforms/javascript/guides/fastify/features/index.mdx new file mode 100644 index 0000000000000..b7fcfb338bb49 --- /dev/null +++ b/docs/platforms/javascript/guides/fastify/features/index.mdx @@ -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. + +