Skip to content

Commit

Permalink
feat(express): Allow to pass options to setupExpressErrorHandler
Browse files Browse the repository at this point in the history
Allows to pass this through to the underlying `expressErrorHandler`.
  • Loading branch information
mydea committed Jul 19, 2024
1 parent cf6eb01 commit aecfb71
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
transport: loggingTransport,
});

// express must be required after Sentry is initialized
const express = require('express');
const cors = require('cors');
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');

const app = express();

app.use(cors());

app.get('/test1', (_req, _res) => {
throw new Error('error_1');
});

app.get('/test2', (_req, _res) => {
throw new Error('error_2');
});

Sentry.setupExpressErrorHandler(app, {
shouldHandleError: error => {
return error.message === 'error_2';
},
});

startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

describe('express setupExpressErrorHandler', () => {
afterAll(() => {
cleanupChildProcesses();
});

describe('CJS', () => {
test('allows to pass options to setupExpressErrorHandler', done => {
const runner = createRunner(__dirname, 'server.js')
.expect({
event: {
exception: {
values: [
{
value: 'error_2',
},
],
},
},
})
.start(done);

// this error is filtered & ignored
expect(() => runner.makeRequest('get', '/test1')).rejects.toThrow();
// this error is actually captured
expect(() => runner.makeRequest('get', '/test2')).rejects.toThrow();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const app = express();
app.use(cors());

app.get('/test/:id1/:id2', (_req, res) => {
Sentry.captureMessage(new Error('error_1'));
Sentry.captureException(new Error('error_1'));
res.send('Success');
});

Expand Down
7 changes: 5 additions & 2 deletions packages/node/src/integrations/tracing/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ export function expressErrorHandler(options?: {
* Setup an error handler for Express.
* The error handler must be before any other middleware and after all controllers.
*/
export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void {
app.use(expressErrorHandler());
export function setupExpressErrorHandler(
app: { use: (middleware: ExpressMiddleware) => unknown },
options?: Parameters<typeof expressErrorHandler>[0],
): void {
app.use(expressErrorHandler(options));
ensureIsWrapped(app.use, 'express');
}

Expand Down

0 comments on commit aecfb71

Please sign in to comment.