Skip to content

Commit 31d50b1

Browse files
committed
Rename option to shouldHandleDiagnosticsChannelError
1 parent f513f8a commit 31d50b1

File tree

2 files changed

+40
-20
lines changed
  • dev-packages/e2e-tests/test-applications/node-fastify-5/src
  • packages/node/src/integrations/tracing/fastify

2 files changed

+40
-20
lines changed

dev-packages/e2e-tests/test-applications/node-fastify-5/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Sentry.init({
1919
dsn: process.env.E2E_TEST_DSN,
2020
integrations: [
2121
Sentry.fastifyIntegration({
22-
shouldHandleError: (error, _request, _reply) => {
22+
shouldHandleDiagnosticsChannelError: (error, _request, _reply) => {
2323
if (_request.routeOptions?.url?.includes('/test-error-not-captured')) {
2424
// Errors from this path will not be captured by Sentry
2525
return false;

packages/node/src/integrations/tracing/fastify/index.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,39 @@ import { FastifyOtelInstrumentation } from './fastify-otel/index';
1717
import type { FastifyInstance, FastifyReply, FastifyRequest } from './types';
1818
import { 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+
2053
interface 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

106126
export 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

Comments
 (0)