Skip to content

Commit eb6a3c5

Browse files
authored
feat(node): Add setupFastifyErrorHandler utility (#11061)
For easier setup of error handling in fastify.
1 parent 2b69368 commit eb6a3c5

File tree

17 files changed

+116
-26
lines changed

17 files changed

+116
-26
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ jobs:
10211021
'sveltekit',
10221022
'sveltekit-2',
10231023
'generic-ts3.8',
1024-
'node-experimental-fastify-app',
1024+
'node-fastify-app',
10251025
# TODO(v8): Re-enable hapi tests
10261026
# 'node-hapi-app',
10271027
'node-exports-test-app',

dev-packages/e2e-tests/test-applications/node-exports-test-app/scripts/consistentExports.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ const DEPENDENTS: Dependent[] = [
4646
package: '@sentry/astro',
4747
compareWith: nodeExports,
4848
exports: Object.keys(SentryAstro),
49+
ignoreExports: [
50+
// Not needed for Astro
51+
'setupFastifyErrorHandler',
52+
],
4953
},
5054
{
5155
package: '@sentry/bun',
@@ -82,13 +86,23 @@ const DEPENDENTS: Dependent[] = [
8286
package: '@sentry/aws-serverless',
8387
compareWith: nodeExports,
8488
exports: Object.keys(SentryAWS),
85-
ignoreExports: ['makeMain'],
89+
ignoreExports: [
90+
// legacy, to be removed...
91+
'makeMain',
92+
// Not needed for Serverless
93+
'setupFastifyErrorHandler',
94+
],
8695
},
8796
{
8897
package: '@sentry/google-cloud-serverless',
8998
compareWith: nodeExports,
9099
exports: Object.keys(SentryGoogleCloud),
91-
ignoreExports: ['makeMain'],
100+
ignoreExports: [
101+
// legacy, to be removed...
102+
'makeMain',
103+
// Not needed for Serverless
104+
'setupFastifyErrorHandler',
105+
],
92106
},
93107
{
94108
package: '@sentry/sveltekit',

dev-packages/e2e-tests/test-applications/node-experimental-fastify-app/package.json renamed to dev-packages/e2e-tests/test-applications/node-fastify-app/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "node-experimental-fastify-app",
2+
"name": "node-fastify-app",
33
"version": "1.0.0",
44
"private": true,
55
"scripts": {
@@ -17,7 +17,6 @@
1717
"@sentry/opentelemetry": "latest || *",
1818
"@types/node": "18.15.1",
1919
"fastify": "4.23.2",
20-
"fastify-plugin": "4.5.1",
2120
"typescript": "4.9.5",
2221
"ts-node": "10.9.1"
2322
},

dev-packages/e2e-tests/test-applications/node-experimental-fastify-app/src/app.js renamed to dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@ require('./tracing');
22

33
const Sentry = require('@sentry/node');
44
const { fastify } = require('fastify');
5-
const fastifyPlugin = require('fastify-plugin');
65
const http = require('http');
76

8-
const FastifySentry = fastifyPlugin(async (fastify, options) => {
9-
fastify.decorateRequest('_sentryContext', null);
10-
11-
fastify.addHook('onError', async (_request, _reply, error) => {
12-
Sentry.captureException(error);
13-
});
14-
});
15-
167
const app = fastify();
178
const port = 3030;
189

19-
app.register(FastifySentry);
10+
Sentry.setupFastifyErrorHandler(app);
2011

2112
app.get('/test-success', function (req, res) {
2213
res.send({ version: 'v1' });
@@ -61,6 +52,10 @@ app.get('/test-error', async function (req, res) {
6152
res.send({ exceptionId });
6253
});
6354

55+
app.get('/test-exception', async function (req, res) {
56+
throw new Error('This is an exception');
57+
});
58+
6459
app.listen({ port: port });
6560

6661
function makeHttpRequest(url) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { startEventProxyServer } from './event-proxy-server';
22

33
startEventProxyServer({
44
port: 3031,
5-
proxyServerName: 'node-experimental-fastify-app',
5+
proxyServerName: 'node-fastify-app',
66
});

0 commit comments

Comments
 (0)