Skip to content

Commit

Permalink
feat(express): Allow to pass options to setupExpressErrorHandler (#…
Browse files Browse the repository at this point in the history
…12952)

Allows to pass this through to the underlying `expressErrorHandler`.

See
#12715 (reply in thread)
  • Loading branch information
mydea authored Jul 19, 2024
1 parent cc50a3a commit 231fc00
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 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
19 changes: 12 additions & 7 deletions packages/node/src/integrations/tracing/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ type ExpressMiddleware = (
next: (error: MiddlewareError) => void,
) => void;

/**
* An Express-compatible error handler.
*/
export function expressErrorHandler(options?: {
interface ExpressHandlerOptions {
/**
* Callback method deciding whether error should be captured and sent to Sentry
* @param error Captured middleware error
*/
shouldHandleError?(this: void, error: MiddlewareError): boolean;
}): ExpressMiddleware {
}

/**
* An Express-compatible error handler.
*/
export function expressErrorHandler(options?: ExpressHandlerOptions): ExpressMiddleware {
return function sentryErrorMiddleware(
error: MiddlewareError,
_req: http.IncomingMessage,
Expand Down Expand Up @@ -135,8 +137,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?: ExpressHandlerOptions,
): void {
app.use(expressErrorHandler(options));
ensureIsWrapped(app.use, 'express');
}

Expand Down

0 comments on commit 231fc00

Please sign in to comment.