Skip to content

feat(node): Add shouldHandleError option to fastifyIntegration #16845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2025
Merged
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 @@ -4,11 +4,13 @@
"private": true,
"scripts": {
"start": "ts-node src/app.ts",
"start:override": "ts-node src/app-handle-error-override.ts",
"test": "playwright test",
"test:override": "playwright test --config playwright.override.config.mjs",
"clean": "npx rimraf node_modules pnpm-lock.yaml",
"typecheck": "tsc",
"test:build": "pnpm install && pnpm run typecheck",
"test:assert": "pnpm test"
"test:assert": "pnpm test && pnpm test:override"
},
"dependencies": {
"@sentry/node": "latest || *",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getPlaywrightConfig } from '@sentry-internal/test-utils';

const config = getPlaywrightConfig({
startCommand: `pnpm start:override`,
});

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import type * as S from '@sentry/node';
const Sentry = require('@sentry/node') as typeof S;

// We wrap console.warn to find out if a warning is incorrectly logged
console.warn = new Proxy(console.warn, {
apply: function (target, thisArg, argumentsList) {
const msg = argumentsList[0];
if (typeof msg === 'string' && msg.startsWith('[Sentry]')) {
console.error(`Sentry warning was triggered: ${msg}`);
process.exit(1);
}

return target.apply(thisArg, argumentsList);
},
});

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [
Sentry.fastifyIntegration({
shouldHandleError: (error, _request, _reply) => {
return true;
},
}),
],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
});

import type * as H from 'http';
import type * as F from 'fastify';

// Make sure fastify is imported after Sentry is initialized
const { fastify } = require('fastify') as typeof F;
const http = require('http') as typeof H;

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;
},
});

app.get('/test-success', function (_req, res) {
res.send({ version: 'v1' });
});

app.get<{ Params: { param: string } }>('/test-param/:param', function (req, res) {
res.send({ paramWas: req.params.param });
});

app.get<{ Params: { id: string } }>('/test-inbound-headers/:id', function (req, res) {
const headers = req.headers;

res.send({ headers, id: req.params.id });
});

app.get<{ Params: { id: string } }>('/test-outgoing-http/:id', async function (req, res) {
const id = req.params.id;
const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`);

res.send(data);
});

app.get<{ Params: { id: string } }>('/test-outgoing-fetch/:id', async function (req, res) {
const id = req.params.id;
const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`);
const data = await response.json();

res.send(data);
});

app.get('/test-transaction', async function (req, res) {
Sentry.startSpan({ name: 'test-span' }, () => {
Sentry.startSpan({ name: 'child-span' }, () => {});
});

res.send({});
});

app.get('/test-error', async function (req, res) {
const exceptionId = Sentry.captureException(new Error('This is an error'));

await Sentry.flush(2000);

res.send({ exceptionId });
});

app.get('/test-error-not-captured', async function () {
// This error will not be captured by Sentry
throw new Error('This is an error that will not be captured');
});

app.get<{ Params: { id: string } }>('/test-exception/:id', async function (req, res) {
throw new Error(`This is an exception with id ${req.params.id}`);
});

app.get('/test-outgoing-fetch-external-allowed', async function (req, res) {
const fetchResponse = await fetch(`http://localhost:${port2}/external-allowed`);
const data = await fetchResponse.json();

res.send(data);
});

app.get('/test-outgoing-fetch-external-disallowed', async function (req, res) {
const fetchResponse = await fetch(`http://localhost:${port2}/external-disallowed`);
const data = await fetchResponse.json();

res.send(data);
});

app.get('/test-outgoing-http-external-allowed', async function (req, res) {
const data = await makeHttpRequest(`http://localhost:${port2}/external-allowed`);
res.send(data);
});

app.get('/test-outgoing-http-external-disallowed', async function (req, res) {
const data = await makeHttpRequest(`http://localhost:${port2}/external-disallowed`);
res.send(data);
});

app.post('/test-post', function (req, res) {
res.send({ status: 'ok', body: req.body });
});

app.listen({ port: port });

// A second app so we can test header propagation between external URLs
const app2 = fastify();
app2.get('/external-allowed', function (req, res) {
const headers = req.headers;

res.send({ headers, route: '/external-allowed' });
});

app2.get('/external-disallowed', function (req, res) {
const headers = req.headers;

res.send({ headers, route: '/external-disallowed' });
});

app2.listen({ port: port2 });

function makeHttpRequest(url: string) {
return new Promise(resolve => {
const data: any[] = [];

http
.request(url, httpRes => {
httpRes.on('data', chunk => {
data.push(chunk);
});
httpRes.on('error', error => {
resolve({ error: error.message, url });
});
httpRes.on('end', () => {
try {
const json = JSON.parse(Buffer.concat(data).toString());
resolve(json);
} catch {
resolve({ data: Buffer.concat(data).toString(), url });
}
});
})
.end();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ console.warn = new Proxy(console.warn, {
Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [],
integrations: [
Sentry.fastifyIntegration({
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;
},
}),
],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
Expand Down Expand Up @@ -81,6 +93,11 @@ app.get('/test-error', async function (req, res) {
res.send({ exceptionId });
});

app.get('/test-error-not-captured', async function () {
// This error will not be captured by Sentry
throw new Error('This is an error that will not be captured');
});

app.get<{ Params: { id: string } }>('/test-exception/:id', async function (req, res) {
throw new Error(`This is an exception with id ${req.params.id}`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,18 @@ test('Sends correct error event', async ({ baseURL }) => {
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
});
});

test('Does not send error when shouldHandleError returns false', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-fastify-3', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an error that will not be captured';
});

errorEventPromise.then(() => {
throw new Error('This error should not be captured');
});

await fetch(`${baseURL}/test-error-not-captured`);

// wait for a short time to ensure the error is not captured
await new Promise(resolve => setTimeout(resolve, 1000));
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"private": true,
"scripts": {
"start": "ts-node src/app.ts",
"start:override": "ts-node src/app-handle-error-override.ts",
"test": "playwright test",
"test:override": "playwright test --config playwright.override.config.mjs",
"clean": "npx rimraf node_modules pnpm-lock.yaml",
"typecheck": "tsc",
"test:build": "pnpm install && pnpm run typecheck",
"test:assert": "pnpm test"
"test:assert": "pnpm test && pnpm test:override"
},
"dependencies": {
"@sentry/node": "latest || *",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getPlaywrightConfig } from '@sentry-internal/test-utils';

const config = getPlaywrightConfig({
startCommand: `pnpm start:override`,
});

export default config;
Loading
Loading