From 1178ba0843cc2845b4275fe612918fdcbfdb2e88 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 23 Feb 2022 16:48:09 -0500 Subject: [PATCH 1/2] ref(browser): Make attachStacktrace usage more explicit Instead of passing down options objects, we instead directly pass down options as appropriate. This makes it very clear how they are being used. Also helps saves on bundle size slightly. --- packages/browser/src/backend.ts | 4 +-- packages/browser/src/eventbuilder.ts | 34 ++++++++----------- .../src/integrations/globalhandlers.ts | 10 ++---- packages/browser/src/parsers.ts | 6 ++-- 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/packages/browser/src/backend.ts b/packages/browser/src/backend.ts index 88d0bba7bc75..3ca50f2b2fe7 100644 --- a/packages/browser/src/backend.ts +++ b/packages/browser/src/backend.ts @@ -40,13 +40,13 @@ export class BrowserBackend extends BaseBackend { * @inheritDoc */ public eventFromException(exception: unknown, hint?: EventHint): PromiseLike { - return eventFromException(this._options, exception, hint); + return eventFromException(exception, hint, this._options.attachStacktrace); } /** * @inheritDoc */ public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike { - return eventFromMessage(this._options, message, level, hint); + return eventFromMessage(message, level, hint, this._options.attachStacktrace); } /** diff --git a/packages/browser/src/eventbuilder.ts b/packages/browser/src/eventbuilder.ts index 051cec270ca4..ffe4a1f59e1d 100644 --- a/packages/browser/src/eventbuilder.ts +++ b/packages/browser/src/eventbuilder.ts @@ -17,11 +17,13 @@ import { eventFromError, eventFromPlainObject, parseStackFrames } from './parser * Creates an {@link Event} from all inputs to `captureException` and non-primitive inputs to `captureMessage`. * @hidden */ -export function eventFromException(options: Options, exception: unknown, hint?: EventHint): PromiseLike { +export function eventFromException( + exception: unknown, + hint?: EventHint, + attachStacktrace?: Options['attachStacktrace'], +): PromiseLike { const syntheticException = (hint && hint.syntheticException) || undefined; - const event = eventFromUnknownInput(exception, syntheticException, { - attachStacktrace: options.attachStacktrace, - }); + const event = eventFromUnknownInput(exception, syntheticException, attachStacktrace); addExceptionMechanism(event); // defaults to { type: 'generic', handled: true } event.level = Severity.Error; if (hint && hint.event_id) { @@ -35,15 +37,13 @@ export function eventFromException(options: Options, exception: unknown, hint?: * @hidden */ export function eventFromMessage( - options: Options, message: string, level: Severity = Severity.Info, hint?: EventHint, + attachStacktrace?: Options['attachStacktrace'], ): PromiseLike { const syntheticException = (hint && hint.syntheticException) || undefined; - const event = eventFromString(message, syntheticException, { - attachStacktrace: options.attachStacktrace, - }); + const event = eventFromString(message, syntheticException, attachStacktrace); event.level = level; if (hint && hint.event_id) { event.event_id = hint.event_id; @@ -57,10 +57,8 @@ export function eventFromMessage( export function eventFromUnknownInput( exception: unknown, syntheticException?: Error, - options: { - isRejection?: boolean; - attachStacktrace?: boolean; - } = {}, + attachStacktrace?: Options['attachStacktrace'], + isUnhandledRejection?: boolean, ): Event { let event: Event; @@ -85,7 +83,7 @@ export function eventFromUnknownInput( } else { const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException'); const message = domException.message ? `${name}: ${domException.message}` : name; - event = eventFromString(message, syntheticException, options); + event = eventFromString(message, syntheticException, attachStacktrace); addExceptionTypeValue(event, message); } if ('code' in domException) { @@ -103,7 +101,7 @@ export function eventFromUnknownInput( // it manually. This will allow us to group events based on top-level keys which is much better than creating a new // group on any key/value change. const objectException = exception as Record; - event = eventFromPlainObject(objectException, syntheticException, options.isRejection); + event = eventFromPlainObject(objectException, syntheticException, isUnhandledRejection); addExceptionMechanism(event, { synthetic: true, }); @@ -119,7 +117,7 @@ export function eventFromUnknownInput( // - a plain Object // // So bail out and capture it as a simple message: - event = eventFromString(exception as string, syntheticException, options); + event = eventFromString(exception as string, syntheticException, attachStacktrace); addExceptionTypeValue(event, `${exception}`, undefined); addExceptionMechanism(event, { synthetic: true, @@ -134,15 +132,13 @@ export function eventFromUnknownInput( export function eventFromString( input: string, syntheticException?: Error, - options: { - attachStacktrace?: boolean; - } = {}, + attachStacktrace?: Options['attachStacktrace'], ): Event { const event: Event = { message: input, }; - if (options.attachStacktrace && syntheticException) { + if (attachStacktrace && syntheticException) { const frames = parseStackFrames(syntheticException); if (frames.length) { event.stacktrace = { frames }; diff --git a/packages/browser/src/integrations/globalhandlers.ts b/packages/browser/src/integrations/globalhandlers.ts index 07d8208f1f66..c3cb56a8f341 100644 --- a/packages/browser/src/integrations/globalhandlers.ts +++ b/packages/browser/src/integrations/globalhandlers.ts @@ -92,10 +92,7 @@ function _installGlobalOnErrorHandler(): void { error === undefined && isString(msg) ? _eventFromIncompleteOnError(msg, url, line, column) : _enhanceEventWithInitialFrame( - eventFromUnknownInput(error || msg, undefined, { - attachStacktrace, - isRejection: false, - }), + eventFromUnknownInput(error || msg, undefined, attachStacktrace, false), url, line, column, @@ -145,10 +142,7 @@ function _installGlobalOnUnhandledRejectionHandler(): void { const event = isPrimitive(error) ? _eventFromRejectionWithPrimitive(error) - : eventFromUnknownInput(error, undefined, { - attachStacktrace, - isRejection: true, - }); + : eventFromUnknownInput(error, undefined, attachStacktrace, true); event.level = Severity.Error; diff --git a/packages/browser/src/parsers.ts b/packages/browser/src/parsers.ts index 0fbefca98b60..ff161c0ec3c3 100644 --- a/packages/browser/src/parsers.ts +++ b/packages/browser/src/parsers.ts @@ -35,15 +35,15 @@ export function exceptionFromError(ex: Error): Exception { export function eventFromPlainObject( exception: Record, syntheticException?: Error, - rejection?: boolean, + isUnhandledRejection?: boolean, ): Event { const event: Event = { exception: { values: [ { - type: isEvent(exception) ? exception.constructor.name : rejection ? 'UnhandledRejection' : 'Error', + type: isEvent(exception) ? exception.constructor.name : isUnhandledRejection ? 'UnhandledRejection' : 'Error', value: `Non-Error ${ - rejection ? 'promise rejection' : 'exception' + isUnhandledRejection ? 'promise rejection' : 'exception' } captured with keys: ${extractExceptionKeysForMessage(exception)}`, }, ], From 626408d81f1038e3a332a350da2403a97e34db29 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 24 Feb 2022 13:11:56 -0500 Subject: [PATCH 2/2] convert to boolean --- packages/browser/src/eventbuilder.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/browser/src/eventbuilder.ts b/packages/browser/src/eventbuilder.ts index ffe4a1f59e1d..ef615d2cedcf 100644 --- a/packages/browser/src/eventbuilder.ts +++ b/packages/browser/src/eventbuilder.ts @@ -1,4 +1,4 @@ -import { Event, EventHint, Options, Severity } from '@sentry/types'; +import { Event, EventHint, Severity } from '@sentry/types'; import { addExceptionMechanism, addExceptionTypeValue, @@ -20,7 +20,7 @@ import { eventFromError, eventFromPlainObject, parseStackFrames } from './parser export function eventFromException( exception: unknown, hint?: EventHint, - attachStacktrace?: Options['attachStacktrace'], + attachStacktrace?: boolean, ): PromiseLike { const syntheticException = (hint && hint.syntheticException) || undefined; const event = eventFromUnknownInput(exception, syntheticException, attachStacktrace); @@ -40,7 +40,7 @@ export function eventFromMessage( message: string, level: Severity = Severity.Info, hint?: EventHint, - attachStacktrace?: Options['attachStacktrace'], + attachStacktrace?: boolean, ): PromiseLike { const syntheticException = (hint && hint.syntheticException) || undefined; const event = eventFromString(message, syntheticException, attachStacktrace); @@ -57,7 +57,7 @@ export function eventFromMessage( export function eventFromUnknownInput( exception: unknown, syntheticException?: Error, - attachStacktrace?: Options['attachStacktrace'], + attachStacktrace?: boolean, isUnhandledRejection?: boolean, ): Event { let event: Event; @@ -129,11 +129,7 @@ export function eventFromUnknownInput( /** * @hidden */ -export function eventFromString( - input: string, - syntheticException?: Error, - attachStacktrace?: Options['attachStacktrace'], -): Event { +export function eventFromString(input: string, syntheticException?: Error, attachStacktrace?: boolean): Event { const event: Event = { message: input, };