From 0459800fa3094753e3606fe642e4b6e06a4c1e3b Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 2 Aug 2024 10:38:05 -0400 Subject: [PATCH] feat(node): Add shouldHandleError option to fastify error handler --- .../node/src/integrations/tracing/fastify.ts | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/node/src/integrations/tracing/fastify.ts b/packages/node/src/integrations/tracing/fastify.ts index 27657d94d3d3..470e90becc02 100644 --- a/packages/node/src/integrations/tracing/fastify.ts +++ b/packages/node/src/integrations/tracing/fastify.ts @@ -61,14 +61,27 @@ const _fastifyIntegration = (() => { */ export const fastifyIntegration = defineIntegration(_fastifyIntegration); +interface FastifyHandlerOptions { + /** + * Callback method deciding whether error should be captured and sent to Sentry + * @param error Captured middleware error + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + shouldHandleError?(this: void, request: any, reply: any, error: Error): boolean; +} + /** * Setup an error handler for Fastify. */ -export function setupFastifyErrorHandler(fastify: Fastify): void { +export function setupFastifyErrorHandler(fastify: Fastify, options?: FastifyHandlerOptions): void { const plugin = Object.assign( function (fastify: Fastify, _options: unknown, done: () => void): void { - fastify.addHook('onError', async (_request, _reply, error) => { - captureException(error); + const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError; + + fastify.addHook('onError', async (request, reply, error) => { + if (shouldHandleError(request, reply, error)) { + captureException(error, { mechanism: { type: 'middleware', handled: false } }); + } }); // registering `onRequest` hook here instead of using Otel `onRequest` callback b/c `onRequest` hook @@ -131,3 +144,10 @@ function addFastifySpanAttributes(span: Span): void { span.updateName(name.replace(/^fastify -> /, '')); } } + +/** Returns true if response code is internal server error */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function defaultShouldHandleError(_request: any, reply: any, _error: Error): boolean { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return reply.statusCode >= 500; +}