Skip to content

Commit e1f81a1

Browse files
committed
feat(express): Allow to pass options to setupExpressErrorHandler
Allows to pass this through to the underlying `expressErrorHandler`.
1 parent 36d1d43 commit e1f81a1

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ export function expressErrorHandler(options?: {
135135
* Setup an error handler for Express.
136136
* The error handler must be before any other middleware and after all controllers.
137137
*/
138-
export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void {
139-
app.use(expressErrorHandler());
138+
export function setupExpressErrorHandler(
139+
app: { use: (middleware: ExpressMiddleware) => unknown },
140+
options?: Parameters<typeof expressErrorHandler>[0],
141+
): void {
142+
app.use(expressErrorHandler(options));
140143
ensureIsWrapped(app.use, 'express');
141144
}
142145

0 commit comments

Comments
 (0)