Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(express): Allow to pass options to setupExpressErrorHandler #12952

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading