Skip to content

Commit

Permalink
fix: Attach stacktrace to message events only when configured by client
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Oct 3, 2019
1 parent 0c83dbc commit 27a26ca
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
8 changes: 6 additions & 2 deletions packages/browser/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
*/
public eventFromException(exception: any, hint?: EventHint): Promise<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromUnknownInput(exception, syntheticException);
const event = eventFromUnknownInput(exception, syntheticException, {
attachStacktrace: this._options.attachStacktrace,
});
addExceptionMechanism(event, {
handled: true,
type: 'generic',
Expand All @@ -74,7 +76,9 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
*/
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): Promise<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromString(message, syntheticException);
const event = eventFromString(message, syntheticException, {
attachStacktrace: this._options.attachStacktrace,
});
event.level = level;
if (hint && hint.event_id) {
event.event_id = hint.event_id;
Expand Down
27 changes: 20 additions & 7 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import { eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from
import { computeStackTrace } from './tracekit';

/** JSDoc */
export function eventFromUnknownInput(exception: unknown, syntheticException?: Error, rejection?: boolean): Event {
export function eventFromUnknownInput(
exception: unknown,
syntheticException?: Error,
options: {
rejection?: boolean;
attachStacktrace?: boolean;
} = {},
): Event {
let event: Event;

if (isErrorEvent(exception as ErrorEvent) && (exception as ErrorEvent).error) {
Expand All @@ -33,7 +40,7 @@ export function eventFromUnknownInput(exception: unknown, syntheticException?: E
const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');
const message = domException.message ? `${name}: ${domException.message}` : name;

event = eventFromString(message);
event = eventFromString(message, syntheticException, options);
addExceptionTypeValue(event, message);
return event;
}
Expand All @@ -47,7 +54,7 @@ export function eventFromUnknownInput(exception: unknown, syntheticException?: E
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
const objectException = exception as {};
event = eventFromPlainObject(objectException, syntheticException, rejection);
event = eventFromPlainObject(objectException, syntheticException, options.rejection);
addExceptionMechanism(event, {
synthetic: true,
});
Expand All @@ -63,7 +70,7 @@ export function eventFromUnknownInput(exception: unknown, syntheticException?: E
// - a plain Object
//
// So bail out and capture it as a simple message:
event = eventFromString(exception as string, syntheticException);
event = eventFromString(exception as string, syntheticException, options);
addExceptionTypeValue(event, `${exception}`, undefined);
addExceptionMechanism(event, {
synthetic: true,
Expand All @@ -72,14 +79,20 @@ export function eventFromUnknownInput(exception: unknown, syntheticException?: E
return event;
}

// this._options.attachStacktrace
/** JSDoc */
export function eventFromString(input: string, syntheticException?: Error): Event {
export function eventFromString(
input: string,
syntheticException?: Error,
options: {
attachStacktrace?: boolean;
} = {},
): Event {
const event: Event = {
message: input,
};

// TODO: Only if `this._options.attachStacktrace`
if (syntheticException) {
if (options.attachStacktrace && syntheticException) {
const stacktrace = computeStackTrace(syntheticException);
const frames = prepareFramesForEvent(stacktrace.stack);
event.stacktrace = {
Expand Down
27 changes: 21 additions & 6 deletions packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export class GlobalHandlers implements Integration {
this._oldOnErrorHandler = this._global.onerror;

this._global.onerror = function(msg: any, url: any, line: any, column: any, error: any): boolean {
const hasIntegration = getCurrentHub().getIntegration(GlobalHandlers);
const currentHub = getCurrentHub();
const hasIntegration = currentHub.getIntegration(GlobalHandlers);
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;

if (!hasIntegration || shouldIgnoreOnError() || isFailedOwnDelivery) {
Expand All @@ -94,16 +95,25 @@ export class GlobalHandlers implements Integration {
return false;
}

const client = currentHub.getClient();
const event = isPrimitive(error)
? self._eventFromIncompleteOnError(msg, url, line, column)
: self._enhanceEventWithInitialFrame(eventFromUnknownInput(error, undefined), url, line, column);
: self._enhanceEventWithInitialFrame(
eventFromUnknownInput(error, undefined, {
attachStacktrace: client && client.getOptions().attachStacktrace,
rejection: false,
}),
url,
line,
column,
);

addExceptionMechanism(event, {
handled: false,
type: 'onerror',
});

getCurrentHub().captureEvent(event, {
currentHub.captureEvent(event, {
originalException: error,
});

Expand Down Expand Up @@ -134,7 +144,8 @@ export class GlobalHandlers implements Integration {
// no-empty
}

const hasIntegration = getCurrentHub().getIntegration(GlobalHandlers);
const currentHub = getCurrentHub();
const hasIntegration = currentHub.getIntegration(GlobalHandlers);
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;

if (!hasIntegration || shouldIgnoreOnError() || isFailedOwnDelivery) {
Expand All @@ -144,9 +155,13 @@ export class GlobalHandlers implements Integration {
return false;
}

const client = currentHub.getClient();
const event = isPrimitive(error)
? self._eventFromIncompleteRejection(error)
: eventFromUnknownInput(error, undefined, true);
: eventFromUnknownInput(error, undefined, {
attachStacktrace: client && client.getOptions().attachStacktrace,
rejection: true,
});

event.level = Severity.Error;

Expand All @@ -155,7 +170,7 @@ export class GlobalHandlers implements Integration {
type: 'onunhandledrejection',
});

getCurrentHub().captureEvent(event, {
currentHub.captureEvent(event, {
originalException: error,
});

Expand Down

0 comments on commit 27a26ca

Please sign in to comment.