From 3e8d0525b40907ab7aa40eacac14a6c74d16fd90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Tue, 2 Jul 2019 17:39:20 +0200 Subject: [PATCH 1/2] feat: Provide optional shouldHandleError option for node errorHandler --- packages/browser/src/integrations/trycatch.ts | 1 + packages/node/src/handlers.ts | 41 +++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/browser/src/integrations/trycatch.ts b/packages/browser/src/integrations/trycatch.ts index 94fe5863a0af..5bcfdc97b9c4 100644 --- a/packages/browser/src/integrations/trycatch.ts +++ b/packages/browser/src/integrations/trycatch.ts @@ -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: { diff --git a/packages/node/src/handlers.ts b/packages/node/src/handlers.ts index 83cb08f227a0..62fb9baf3a26 100644 --- a/packages/node/src/handlers.ts +++ b/packages/node/src/handlers.ts @@ -272,11 +272,23 @@ function getStatusCodeFromResponse(error: MiddlewareError): number { return statusCode ? parseInt(statusCode as string, 10) : 500; } +/** JSDoc */ +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, @@ -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); }; } From 874610f7c24a6fc165b055dc40cc1d957807ac8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Fri, 5 Jul 2019 10:29:16 +0200 Subject: [PATCH 2/2] Update packages/node/src/handlers.ts Co-Authored-By: Daniel Griesser --- packages/node/src/handlers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/src/handlers.ts b/packages/node/src/handlers.ts index 62fb9baf3a26..9e9e81a67f3c 100644 --- a/packages/node/src/handlers.ts +++ b/packages/node/src/handlers.ts @@ -272,7 +272,7 @@ function getStatusCodeFromResponse(error: MiddlewareError): number { return statusCode ? parseInt(statusCode as string, 10) : 500; } -/** JSDoc */ +/** Returns true if response code is internal server error */ function defaultShouldHandleError(error: MiddlewareError): boolean { const status = getStatusCodeFromResponse(error); return status >= 500;