Skip to content

Commit 231fc00

Browse files
authored
feat(express): Allow to pass options to setupExpressErrorHandler (#12952)
Allows to pass this through to the underlying `expressErrorHandler`. See #12715 (reply in thread)
1 parent cc50a3a commit 231fc00

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
transport: loggingTransport,
8+
});
9+
10+
// express must be required after Sentry is initialized
11+
const express = require('express');
12+
const cors = require('cors');
13+
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');
14+
15+
const app = express();
16+
17+
app.use(cors());
18+
19+
app.get('/test1', (_req, _res) => {
20+
throw new Error('error_1');
21+
});
22+
23+
app.get('/test2', (_req, _res) => {
24+
throw new Error('error_2');
25+
});
26+
27+
Sentry.setupExpressErrorHandler(app, {
28+
shouldHandleError: error => {
29+
return error.message === 'error_2';
30+
},
31+
});
32+
33+
startExpressServerAndSendPortToRunner(app);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
3+
describe('express setupExpressErrorHandler', () => {
4+
afterAll(() => {
5+
cleanupChildProcesses();
6+
});
7+
8+
describe('CJS', () => {
9+
test('allows to pass options to setupExpressErrorHandler', done => {
10+
const runner = createRunner(__dirname, 'server.js')
11+
.expect({
12+
event: {
13+
exception: {
14+
values: [
15+
{
16+
value: 'error_2',
17+
},
18+
],
19+
},
20+
},
21+
})
22+
.start(done);
23+
24+
// this error is filtered & ignored
25+
expect(() => runner.makeRequest('get', '/test1')).rejects.toThrow();
26+
// this error is actually captured
27+
expect(() => runner.makeRequest('get', '/test2')).rejects.toThrow();
28+
});
29+
});
30+
});

dev-packages/node-integration-tests/suites/express/tracing/withError/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const app = express();
2020
app.use(cors());
2121

2222
app.get('/test/:id1/:id2', (_req, res) => {
23-
Sentry.captureMessage(new Error('error_1'));
23+
Sentry.captureException(new Error('error_1'));
2424
res.send('Success');
2525
});
2626

packages/node/src/integrations/tracing/express.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,18 @@ type ExpressMiddleware = (
8383
next: (error: MiddlewareError) => void,
8484
) => void;
8585

86-
/**
87-
* An Express-compatible error handler.
88-
*/
89-
export function expressErrorHandler(options?: {
86+
interface ExpressHandlerOptions {
9087
/**
9188
* Callback method deciding whether error should be captured and sent to Sentry
9289
* @param error Captured middleware error
9390
*/
9491
shouldHandleError?(this: void, error: MiddlewareError): boolean;
95-
}): ExpressMiddleware {
92+
}
93+
94+
/**
95+
* An Express-compatible error handler.
96+
*/
97+
export function expressErrorHandler(options?: ExpressHandlerOptions): ExpressMiddleware {
9698
return function sentryErrorMiddleware(
9799
error: MiddlewareError,
98100
_req: http.IncomingMessage,
@@ -135,8 +137,11 @@ export function expressErrorHandler(options?: {
135137
* Setup an error handler for Express.
136138
* The error handler must be before any other middleware and after all controllers.
137139
*/
138-
export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void {
139-
app.use(expressErrorHandler());
140+
export function setupExpressErrorHandler(
141+
app: { use: (middleware: ExpressMiddleware) => unknown },
142+
options?: ExpressHandlerOptions,
143+
): void {
144+
app.use(expressErrorHandler(options));
140145
ensureIsWrapped(app.use, 'express');
141146
}
142147

0 commit comments

Comments
 (0)