Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
}),
],
Expand All @@ -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' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Sentry.init({
integrations: [
Sentry.fastifyIntegration({
shouldHandleError: (error, _request, _reply) => {
return true;
return !_request.routeOptions?.url?.includes('/test-error-not-captured');
},
}),
],
Expand All @@ -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' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
}),
],
Expand All @@ -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' });
Expand Down
23 changes: 22 additions & 1 deletion docs/migration/v11-end-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
66 changes: 2 additions & 64 deletions packages/node/src/integrations/tracing/fastify/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
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,
instrumentFastify,
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 };

Expand Down Expand Up @@ -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/).
*
Expand All @@ -125,7 +68,6 @@ export const fastifyIntegration = defineIntegration((options: Partial<FastifyInt
* Add an Fastify error handler to capture errors to Sentry.
*
* @param fastify The Fastify instance to which to add the error handler
* @param options Configuration options for the handler
*
* @example
* ```javascript
Expand All @@ -141,11 +83,7 @@ export const fastifyIntegration = defineIntegration((options: Partial<FastifyInt
* app.listen({ port: 3000 });
* ```
*/
export function setupFastifyErrorHandler(fastify: FastifyMinimal, options?: Partial<FastifyHandlerOptions>): 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) => {
Expand Down
3 changes: 0 additions & 3 deletions packages/server-utils/src/integrations/fastify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ const _fastifyIntegration = (({ shouldHandleError }: Partial<FastifyIntegrationO
getShouldHandleError() {
return _shouldHandleError;
},
setShouldHandleError(shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean) {
_shouldHandleError = shouldHandleError;
},
} satisfies FastifyIntegration;
}) satisfies IntegrationFn;

Expand Down
4 changes: 0 additions & 4 deletions packages/server-utils/src/integrations/fastify/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading