@@ -17,6 +17,39 @@ import { FastifyOtelInstrumentation } from './fastify-otel/index';
1717import type { FastifyInstance , FastifyReply , FastifyRequest } from './types' ;
1818import { FastifyInstrumentationV3 } from './v3/instrumentation' ;
1919
20+ /**
21+ * Options for the Fastify integration.
22+ *
23+ * `shouldHandleDiagnosticsChannelError` - Callback method deciding whether error should be captured and sent to Sentry
24+ * This is used on Fastify v5 where Sentry handles errors in the diagnostics channel.
25+ * Fastify v3 and v4 use `setupFastifyErrorHandler` instead.
26+ *
27+ * @example
28+ *
29+ * ```javascript
30+ * Sentry.init({
31+ * integrations: [
32+ * Sentry.fastifyIntegration({
33+ * shouldHandleDiagnosticsChannelError(_error, _request, reply) {
34+ * return reply.statusCode >= 500;
35+ * },
36+ * });
37+ * },
38+ * });
39+ * ```
40+ *
41+ */
42+ interface FastifyIntegrationOptions {
43+ /**
44+ * Callback method deciding whether error should be captured and sent to Sentry
45+ *
46+ * @param error Captured Fastify error
47+ * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath)
48+ * @param reply Fastify reply (or any object containing at least statusCode)
49+ */
50+ shouldHandleDiagnosticsChannelError : ( error : Error , request : FastifyRequest , reply : FastifyReply ) => boolean ;
51+ }
52+
2053interface FastifyHandlerOptions {
2154 /**
2255 * Callback method deciding whether error should be captured and sent to Sentry
@@ -36,19 +69,6 @@ interface FastifyHandlerOptions {
3669 * });
3770 * ```
3871 *
39- * or if you use Fastify v5 you can set options in the Sentry.init call:
40- *
41- * ```javascript
42- * Sentry.init({
43- * integrations: [
44- * Sentry.fastifyIntegration({
45- * shouldHandleError(_error, _request, reply) {
46- * return reply.statusCode >= 500;
47- * },
48- * });
49- * },
50- * });
51- * ```
5272 *
5373 * If using TypeScript, you can cast the request and reply to get full type safety.
5474 *
@@ -105,7 +125,7 @@ function handleFastifyError(
105125
106126export const instrumentFastify = generateInstrumentOnce (
107127 INTEGRATION_NAME ,
108- ( options : Partial < FastifyHandlerOptions > = { } ) => {
128+ ( options : Partial < FastifyIntegrationOptions > = { } ) => {
109129 const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation ( ) ;
110130 const plugin = fastifyOtelInstrumentationInstance . plugin ( ) ;
111131
@@ -140,23 +160,23 @@ export const instrumentFastify = generateInstrumentOnce(
140160 error ,
141161 request ,
142162 reply ,
143- options ?. shouldHandleError || defaultShouldHandleError ,
163+ options ?. shouldHandleDiagnosticsChannelError || defaultShouldHandleError ,
144164 'diagnostics-channel' ,
145165 ) ;
146166 } ) ;
147167
148168 // Returning this as unknown not to deal with the internal types of the FastifyOtelInstrumentation
149- return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyHandlerOptions > ;
169+ return fastifyOtelInstrumentationInstance as Instrumentation < InstrumentationConfig & FastifyIntegrationOptions > ;
150170 } ,
151171) ;
152172
153- const _fastifyIntegration = ( ( { shouldHandleError } ) => {
173+ const _fastifyIntegration = ( ( { shouldHandleDiagnosticsChannelError } : Partial < FastifyIntegrationOptions > ) => {
154174 return {
155175 name : INTEGRATION_NAME ,
156176 setupOnce ( ) {
157177 instrumentFastifyV3 ( ) ;
158178 instrumentFastify ( {
159- shouldHandleError ,
179+ shouldHandleDiagnosticsChannelError ,
160180 } ) ;
161181 } ,
162182 } ;
@@ -178,7 +198,7 @@ const _fastifyIntegration = (({ shouldHandleError }) => {
178198 * })
179199 * ```
180200 */
181- export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyHandlerOptions > = { } ) =>
201+ export const fastifyIntegration = defineIntegration ( ( options : Partial < FastifyIntegrationOptions > = { } ) =>
182202 _fastifyIntegration ( options ) ,
183203) ;
184204
0 commit comments