Skip to content
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
1 change: 1 addition & 0 deletions packages/browser/src/integrations/trycatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class TryCatch implements Integration {
options?: boolean | AddEventListenerOptions,
): (eventName: string, fn: EventListenerObject, capture?: boolean, secure?: boolean) => void {
try {
// tslint:disable-next-line:no-unbound-method strict-type-predicates
if (typeof fn.handleEvent === 'function') {
fn.handleEvent = wrap(fn.handleEvent.bind(fn), {
mechanism: {
Expand Down
41 changes: 28 additions & 13 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,23 @@ function getStatusCodeFromResponse(error: MiddlewareError): number {
return statusCode ? parseInt(statusCode as string, 10) : 500;
}

/** Returns true if response code is internal server error */
function defaultShouldHandleError(error: MiddlewareError): boolean {
const status = getStatusCodeFromResponse(error);
return status >= 500;
}

/**
* Express compatible error handler.
* @see Exposed as `Handlers.errorHandler`
*/
export function errorHandler(): (
export function errorHandler(options?: {
/**
* Callback method deciding whether error should be captured and sent to Sentry
* @param error Captured middleware error
*/
shouldHandleError?(error: MiddlewareError): boolean;
}): (
error: MiddlewareError,
req: http.IncomingMessage,
res: http.ServerResponse,
Expand All @@ -288,20 +300,23 @@ export function errorHandler(): (
_res: http.ServerResponse,
next: (error: MiddlewareError) => void,
): void {
const status = getStatusCodeFromResponse(error);
if (status < 500) {
next(error);
const shouldHandleError = (options && options.shouldHandleError) || defaultShouldHandleError;

if (shouldHandleError(error)) {
withScope(scope => {
if (_req.headers && isString(_req.headers['sentry-trace'])) {
const span = Span.fromTraceparent(_req.headers['sentry-trace'] as string);
scope.setSpan(span);
}
const eventId = captureException(error);
(_res as any).sentry = eventId;
next(error);
});

return;
}
withScope(scope => {
if (_req.headers && isString(_req.headers['sentry-trace'])) {
const span = Span.fromTraceparent(_req.headers['sentry-trace'] as string);
scope.setSpan(span);
}
const eventId = captureException(error);
(_res as any).sentry = eventId;
next(error);
});

next(error);
};
}

Expand Down