diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-3/src/app-handle-error-override.ts b/dev-packages/e2e-tests/test-applications/node-fastify-3/src/app-handle-error-override.ts index 7f0890002772..582cfd57523d 100644 --- a/dev-packages/e2e-tests/test-applications/node-fastify-3/src/app-handle-error-override.ts +++ b/dev-packages/e2e-tests/test-applications/node-fastify-3/src/app-handle-error-override.ts @@ -21,7 +21,8 @@ Sentry.init({ integrations: [ Sentry.fastifyIntegration({ shouldHandleError: (error, _request, _reply) => { - return true; + // @ts-ignore // Fastify V3 is not typed correctly + return !_request.url?.includes('/test-error-not-captured'); }, }), ], @@ -41,17 +42,7 @@ const app = fastify(); const port = 3030; const port2 = 3040; -Sentry.setupFastifyErrorHandler(app, { - shouldHandleError: (error, _request, _reply) => { - // @ts-ignore // Fastify V3 is not typed correctly - if (_request.url?.includes('/test-error-not-captured')) { - // Errors from this path will not be captured by Sentry - return false; - } - - return true; - }, -}); +Sentry.setupFastifyErrorHandler(app); app.get('/test-success', function (_req, res) { res.send({ version: 'v1' }); diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-4/src/app-handle-error-override.ts b/dev-packages/e2e-tests/test-applications/node-fastify-4/src/app-handle-error-override.ts index 5552e765b98b..2ef56a022dec 100644 --- a/dev-packages/e2e-tests/test-applications/node-fastify-4/src/app-handle-error-override.ts +++ b/dev-packages/e2e-tests/test-applications/node-fastify-4/src/app-handle-error-override.ts @@ -21,7 +21,7 @@ Sentry.init({ integrations: [ Sentry.fastifyIntegration({ shouldHandleError: (error, _request, _reply) => { - return true; + return !_request.routeOptions?.url?.includes('/test-error-not-captured'); }, }), ], @@ -41,16 +41,7 @@ const app = fastify(); const port = 3030; const port2 = 3040; -Sentry.setupFastifyErrorHandler(app, { - shouldHandleError: (error, _request, _reply) => { - if (_request.routeOptions?.url?.includes('/test-error-not-captured')) { - // Errors from this path will not be captured by Sentry - return false; - } - - return true; - }, -}); +Sentry.setupFastifyErrorHandler(app); app.get('/test-success', function (_req, res) { res.send({ version: 'v1' }); diff --git a/dev-packages/e2e-tests/test-applications/node-fastify-5/src/app-handle-error-override.ts b/dev-packages/e2e-tests/test-applications/node-fastify-5/src/app-handle-error-override.ts index 30dbbc6cdca0..c01a0bb0b458 100644 --- a/dev-packages/e2e-tests/test-applications/node-fastify-5/src/app-handle-error-override.ts +++ b/dev-packages/e2e-tests/test-applications/node-fastify-5/src/app-handle-error-override.ts @@ -21,7 +21,14 @@ Sentry.init({ integrations: [ Sentry.fastifyIntegration({ shouldHandleError: (error, _request, _reply) => { - return true; + // @ts-ignore // Fastify V5 is not typed correctly + if (_request.routeOptions?.url?.includes('/test-error-not-captured')) { + // Errors from this path will not be captured by Sentry + return false; + } + + // @ts-ignore // Fastify V5 is not typed correctly + return !(_request.routeOptions?.url?.includes('/test-error-ignored') && _reply.statusCode === 500); }, }), ], @@ -41,22 +48,7 @@ const app = fastify(); const port = 3030; const port2 = 3040; -Sentry.setupFastifyErrorHandler(app, { - shouldHandleError: (error, _request, _reply) => { - // @ts-ignore // Fastify V5 is not typed correctly - if (_request.routeOptions?.url?.includes('/test-error-not-captured')) { - // Errors from this path will not be captured by Sentry - return false; - } - - // @ts-ignore // Fastify V5 is not typed correctly - if (_request.routeOptions?.url?.includes('/test-error-ignored') && _reply.statusCode === 500) { - return false; - } - - return true; - }, -}); +Sentry.setupFastifyErrorHandler(app); app.get('/test-success', function (_req, res) { res.send({ version: 'v1' }); diff --git a/docs/migration/v11-end-state.md b/docs/migration/v11-end-state.md index 039b8c4b2146..3a965384ea0f 100644 --- a/docs/migration/v11-end-state.md +++ b/docs/migration/v11-end-state.md @@ -789,7 +789,28 @@ Sentry.init({ - (Next.js) The `@sentry/nextjs/loader` entry point was removed. Use `node --import @sentry/nextjs/import` instead. - (Remix) The `@sentry/remix/loader` entry point was removed. Use `node --import @sentry/remix/import` instead. - (TanStack Start) The `@sentry/tanstackstart-react/loader` entry point was removed. Use `node --import @sentry/tanstackstart-react/import` instead. -- (Fastify) The deprecated `setShouldHandleError` method was removed. +- (Fastify) The deprecated `setShouldHandleError` method was removed. The `shouldHandleError` option was also removed from `setupFastifyErrorHandler`. Configure it on `fastifyIntegration` instead. + +```diff + Sentry.init({ +- integrations: [Sentry.fastifyIntegration()], ++ integrations: [ ++ Sentry.fastifyIntegration({ ++ shouldHandleError(_error, _request, reply) { ++ return reply.statusCode >= 500; ++ }, ++ }), ++ ], + }); + +-Sentry.setupFastifyErrorHandler(app, { +- shouldHandleError(_error, _request, reply) { +- return reply.statusCode >= 500; +- }, +-}); ++Sentry.setupFastifyErrorHandler(app); +``` + - (AWS Lambda) The deprecated `disableAwsContextPropagation` option was removed. It no longer had any effect. - (AWS Lambda) The deprecated `startTrace` option was removed. It no longer had any effect; to disable tracing, set `tracesSampleRate` to `0`. - (AWS Lambda) The deprecated `tryPatchHandler` function was removed. It was no longer used. diff --git a/packages/node/src/integrations/tracing/fastify/index.ts b/packages/node/src/integrations/tracing/fastify/index.ts index 3b4977416908..1e094ee59389 100644 --- a/packages/node/src/integrations/tracing/fastify/index.ts +++ b/packages/node/src/integrations/tracing/fastify/index.ts @@ -1,5 +1,4 @@ -import type { Integration } from '@sentry/core'; -import { defineIntegration, getClient } from '@sentry/core'; +import { defineIntegration } from '@sentry/core'; import type { FastifyInstance, FastifyMinimal, FastifyReply, FastifyRequest } from './types'; import { fastifyIntegration as serverUtilsFastifyIntegration, @@ -7,14 +6,6 @@ import { handleFastifyError, } from '@sentry/server-utils'; -interface FastifyIntegration extends Integration { - getShouldHandleError: () => (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean; - // todo(v11): Remove this - setShouldHandleError: ( - shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean, - ) => void; -} - // oxlint-disable-next-line typescript/no-deprecated export { instrumentFastify }; @@ -53,54 +44,6 @@ interface FastifyIntegrationOptions { shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean; } -interface FastifyHandlerOptions { - /** - * Callback method deciding whether error should be captured and sent to Sentry - * - * @param error Captured Fastify error - * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath) - * @param reply Fastify reply (or any object containing at least statusCode) - * - * @example - * - * - * ```javascript - * setupFastifyErrorHandler(app, { - * shouldHandleError(_error, _request, reply) { - * return reply.statusCode >= 400; - * }, - * }); - * ``` - * - * - * If using TypeScript, you can cast the request and reply to get full type safety. - * - * ```typescript - * import type { FastifyRequest, FastifyReply } from 'fastify'; - * - * setupFastifyErrorHandler(app, { - * shouldHandleError(error, minimalRequest, minimalReply) { - * const request = minimalRequest as FastifyRequest; - * const reply = minimalReply as FastifyReply; - * return reply.statusCode >= 500; - * }, - * }); - * ``` - */ - shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean; -} - -const INTEGRATION_NAME = 'Fastify' as const; - -function getFastifyIntegration(): FastifyIntegration | undefined { - const client = getClient(); - if (!client) { - return undefined; - } else { - return client.getIntegrationByName(INTEGRATION_NAME); - } -} - /** * Adds Sentry tracing instrumentation for [Fastify](https://fastify.dev/). * @@ -125,7 +68,6 @@ export const fastifyIntegration = defineIntegration((options: Partial): void { - if (options?.shouldHandleError) { - getFastifyIntegration()?.setShouldHandleError(options.shouldHandleError); - } - +export function setupFastifyErrorHandler(fastify: FastifyMinimal): void { const plugin = Object.assign( function (fastify: FastifyInstance, _options: unknown, done: () => void): void { fastify.addHook('onError', async (request, reply, error) => { diff --git a/packages/server-utils/src/integrations/fastify/index.ts b/packages/server-utils/src/integrations/fastify/index.ts index c690c4ce7407..b110ea4494de 100644 --- a/packages/server-utils/src/integrations/fastify/index.ts +++ b/packages/server-utils/src/integrations/fastify/index.ts @@ -54,9 +54,6 @@ const _fastifyIntegration = (({ shouldHandleError }: Partial boolean) { - _shouldHandleError = shouldHandleError; - }, } satisfies FastifyIntegration; }) satisfies IntegrationFn; diff --git a/packages/server-utils/src/integrations/fastify/types.ts b/packages/server-utils/src/integrations/fastify/types.ts index cda6fcb3d662..c4828617c67f 100644 --- a/packages/server-utils/src/integrations/fastify/types.ts +++ b/packages/server-utils/src/integrations/fastify/types.ts @@ -41,8 +41,4 @@ export interface FastifyRequest { export interface FastifyIntegration extends Integration { getShouldHandleError: () => (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean; - // This will be removed in the next major version. - setShouldHandleError: ( - shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean, - ) => void; }