GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(express): Allow to pass options to `setupExpressErrorHandler` by mydea · Pull Request #12952 · getsentry/sentry-javascript

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

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.

Sign up for GitHub

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

Merged
mydea merged 2 commits into develop from fn/express-handler-options
Jul 19, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
aecfb71
feat(express): Allow to pass options to `setupExpressErrorHandler`
mydea Jul 17, 2024
3c462a8
define option types better
mydea Jul 17, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

33 changes: 33 additions & 0 deletions 33 dev-packages/node-integration-tests/suites/express/setupExpressErrorHandler/server.js
Show comments
View file
Open in desktop
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);
30 changes: 30 additions & 0 deletions 30 dev-packages/node-integration-tests/suites/express/setupExpressErrorHandler/test.ts
Show comments
View file
Open in desktop
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();
});
});
});
2 changes: 1 addition & 1 deletion 2 dev-packages/node-integration-tests/suites/express/tracing/withError/server.js
Show comments
View file
Open in desktop
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 19 packages/node/src/integrations/tracing/express.ts
Show comments
View file
Open in desktop
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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.