Skip to content

Commit f04f6ac

Browse files
committed
feat(node): Add setupFastifyErrorHandler utility
For easier setup of error handling in fastify.
1 parent 963c3de commit f04f6ac

File tree

3 files changed

+14
-12
lines changed
  • dev-packages/e2e-tests/test-applications/node-experimental-fastify-app/src
  • packages/node-experimental/src

3 files changed

+14
-12
lines changed

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

Lines changed: 1 addition & 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' });

packages/node-experimental/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { onUnhandledRejectionIntegration } from './integrations/onunhandledrejec
1111
export { anrIntegration } from './integrations/anr';
1212

1313
export { expressIntegration, expressErrorHandler, setupExpressErrorHandler } from './integrations/tracing/express';
14-
export { fastifyIntegration } from './integrations/tracing/fastify';
14+
export { fastifyIntegration, setupFastifyErrorHandler } from './integrations/tracing/fastify';
1515
export { graphqlIntegration } from './integrations/tracing/graphql';
1616
export { mongoIntegration } from './integrations/tracing/mongo';
1717
export { mongooseIntegration } from './integrations/tracing/mongoose';

packages/node-experimental/src/integrations/tracing/fastify.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { registerInstrumentations } from '@opentelemetry/instrumentation';
22
import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
3-
import { defineIntegration } from '@sentry/core';
3+
import { captureException, defineIntegration } from '@sentry/core';
44
import type { IntegrationFn } from '@sentry/types';
55

66
import { addOriginToSpan } from '../../utils/addOriginToSpan';
@@ -28,3 +28,14 @@ const _fastifyIntegration = (() => {
2828
* Capture tracing data for fastify.
2929
*/
3030
export const fastifyIntegration = defineIntegration(_fastifyIntegration);
31+
32+
/**
33+
* Setup an error handler for Fastify.
34+
*/
35+
export function setupFastifyErrorHandler(fastify: {
36+
addHook: (hook: string, handler: (request: unknown, reply: unknown, error: Error) => void) => void;
37+
}): void {
38+
fastify.addHook('onError', async (_request, _reply, error) => {
39+
captureException(error);
40+
});
41+
}

0 commit comments

Comments
 (0)