Skip to content

Commit 27a26ca

Browse files
committed
fix: Attach stacktrace to message events only when configured by client
1 parent 0c83dbc commit 27a26ca

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

packages/browser/src/backend.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
5858
*/
5959
public eventFromException(exception: any, hint?: EventHint): Promise<Event> {
6060
const syntheticException = (hint && hint.syntheticException) || undefined;
61-
const event = eventFromUnknownInput(exception, syntheticException);
61+
const event = eventFromUnknownInput(exception, syntheticException, {
62+
attachStacktrace: this._options.attachStacktrace,
63+
});
6264
addExceptionMechanism(event, {
6365
handled: true,
6466
type: 'generic',
@@ -74,7 +76,9 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
7476
*/
7577
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): Promise<Event> {
7678
const syntheticException = (hint && hint.syntheticException) || undefined;
77-
const event = eventFromString(message, syntheticException);
79+
const event = eventFromString(message, syntheticException, {
80+
attachStacktrace: this._options.attachStacktrace,
81+
});
7882
event.level = level;
7983
if (hint && hint.event_id) {
8084
event.event_id = hint.event_id;

packages/browser/src/eventbuilder.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ import { eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from
1414
import { computeStackTrace } from './tracekit';
1515

1616
/** JSDoc */
17-
export function eventFromUnknownInput(exception: unknown, syntheticException?: Error, rejection?: boolean): Event {
17+
export function eventFromUnknownInput(
18+
exception: unknown,
19+
syntheticException?: Error,
20+
options: {
21+
rejection?: boolean;
22+
attachStacktrace?: boolean;
23+
} = {},
24+
): Event {
1825
let event: Event;
1926

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

36-
event = eventFromString(message);
43+
event = eventFromString(message, syntheticException, options);
3744
addExceptionTypeValue(event, message);
3845
return event;
3946
}
@@ -47,7 +54,7 @@ export function eventFromUnknownInput(exception: unknown, syntheticException?: E
4754
// This will allow us to group events based on top-level keys
4855
// which is much better than creating new group when any key/value change
4956
const objectException = exception as {};
50-
event = eventFromPlainObject(objectException, syntheticException, rejection);
57+
event = eventFromPlainObject(objectException, syntheticException, options.rejection);
5158
addExceptionMechanism(event, {
5259
synthetic: true,
5360
});
@@ -63,7 +70,7 @@ export function eventFromUnknownInput(exception: unknown, syntheticException?: E
6370
// - a plain Object
6471
//
6572
// So bail out and capture it as a simple message:
66-
event = eventFromString(exception as string, syntheticException);
73+
event = eventFromString(exception as string, syntheticException, options);
6774
addExceptionTypeValue(event, `${exception}`, undefined);
6875
addExceptionMechanism(event, {
6976
synthetic: true,
@@ -72,14 +79,20 @@ export function eventFromUnknownInput(exception: unknown, syntheticException?: E
7279
return event;
7380
}
7481

82+
// this._options.attachStacktrace
7583
/** JSDoc */
76-
export function eventFromString(input: string, syntheticException?: Error): Event {
84+
export function eventFromString(
85+
input: string,
86+
syntheticException?: Error,
87+
options: {
88+
attachStacktrace?: boolean;
89+
} = {},
90+
): Event {
7791
const event: Event = {
7892
message: input,
7993
};
8094

81-
// TODO: Only if `this._options.attachStacktrace`
82-
if (syntheticException) {
95+
if (options.attachStacktrace && syntheticException) {
8396
const stacktrace = computeStackTrace(syntheticException);
8497
const frames = prepareFramesForEvent(stacktrace.stack);
8598
event.stacktrace = {

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export class GlobalHandlers implements Integration {
8484
this._oldOnErrorHandler = this._global.onerror;
8585

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

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

98+
const client = currentHub.getClient();
9799
const event = isPrimitive(error)
98100
? self._eventFromIncompleteOnError(msg, url, line, column)
99-
: self._enhanceEventWithInitialFrame(eventFromUnknownInput(error, undefined), url, line, column);
101+
: self._enhanceEventWithInitialFrame(
102+
eventFromUnknownInput(error, undefined, {
103+
attachStacktrace: client && client.getOptions().attachStacktrace,
104+
rejection: false,
105+
}),
106+
url,
107+
line,
108+
column,
109+
);
100110

101111
addExceptionMechanism(event, {
102112
handled: false,
103113
type: 'onerror',
104114
});
105115

106-
getCurrentHub().captureEvent(event, {
116+
currentHub.captureEvent(event, {
107117
originalException: error,
108118
});
109119

@@ -134,7 +144,8 @@ export class GlobalHandlers implements Integration {
134144
// no-empty
135145
}
136146

137-
const hasIntegration = getCurrentHub().getIntegration(GlobalHandlers);
147+
const currentHub = getCurrentHub();
148+
const hasIntegration = currentHub.getIntegration(GlobalHandlers);
138149
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;
139150

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

158+
const client = currentHub.getClient();
147159
const event = isPrimitive(error)
148160
? self._eventFromIncompleteRejection(error)
149-
: eventFromUnknownInput(error, undefined, true);
161+
: eventFromUnknownInput(error, undefined, {
162+
attachStacktrace: client && client.getOptions().attachStacktrace,
163+
rejection: true,
164+
});
150165

151166
event.level = Severity.Error;
152167

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

158-
getCurrentHub().captureEvent(event, {
173+
currentHub.captureEvent(event, {
159174
originalException: error,
160175
});
161176

0 commit comments

Comments
 (0)