Skip to content

Commit 196498a

Browse files
authored
ref(browser): Make attachStacktrace usage more explicit (#4628)
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.
1 parent 50a5d9e commit 196498a

File tree

4 files changed

+23
-37
lines changed

4 files changed

+23
-37
lines changed

packages/browser/src/backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
4040
* @inheritDoc
4141
*/
4242
public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> {
43-
return eventFromException(this._options, exception, hint);
43+
return eventFromException(exception, hint, this._options.attachStacktrace);
4444
}
4545
/**
4646
* @inheritDoc
4747
*/
4848
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike<Event> {
49-
return eventFromMessage(this._options, message, level, hint);
49+
return eventFromMessage(message, level, hint, this._options.attachStacktrace);
5050
}
5151

5252
/**

packages/browser/src/eventbuilder.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event, EventHint, Options, Severity } from '@sentry/types';
1+
import { Event, EventHint, Severity } from '@sentry/types';
22
import {
33
addExceptionMechanism,
44
addExceptionTypeValue,
@@ -17,11 +17,13 @@ import { eventFromError, eventFromPlainObject, parseStackFrames } from './parser
1717
* Creates an {@link Event} from all inputs to `captureException` and non-primitive inputs to `captureMessage`.
1818
* @hidden
1919
*/
20-
export function eventFromException(options: Options, exception: unknown, hint?: EventHint): PromiseLike<Event> {
20+
export function eventFromException(
21+
exception: unknown,
22+
hint?: EventHint,
23+
attachStacktrace?: boolean,
24+
): PromiseLike<Event> {
2125
const syntheticException = (hint && hint.syntheticException) || undefined;
22-
const event = eventFromUnknownInput(exception, syntheticException, {
23-
attachStacktrace: options.attachStacktrace,
24-
});
26+
const event = eventFromUnknownInput(exception, syntheticException, attachStacktrace);
2527
addExceptionMechanism(event); // defaults to { type: 'generic', handled: true }
2628
event.level = Severity.Error;
2729
if (hint && hint.event_id) {
@@ -35,15 +37,13 @@ export function eventFromException(options: Options, exception: unknown, hint?:
3537
* @hidden
3638
*/
3739
export function eventFromMessage(
38-
options: Options,
3940
message: string,
4041
level: Severity = Severity.Info,
4142
hint?: EventHint,
43+
attachStacktrace?: boolean,
4244
): PromiseLike<Event> {
4345
const syntheticException = (hint && hint.syntheticException) || undefined;
44-
const event = eventFromString(message, syntheticException, {
45-
attachStacktrace: options.attachStacktrace,
46-
});
46+
const event = eventFromString(message, syntheticException, attachStacktrace);
4747
event.level = level;
4848
if (hint && hint.event_id) {
4949
event.event_id = hint.event_id;
@@ -57,10 +57,8 @@ export function eventFromMessage(
5757
export function eventFromUnknownInput(
5858
exception: unknown,
5959
syntheticException?: Error,
60-
options: {
61-
isRejection?: boolean;
62-
attachStacktrace?: boolean;
63-
} = {},
60+
attachStacktrace?: boolean,
61+
isUnhandledRejection?: boolean,
6462
): Event {
6563
let event: Event;
6664

@@ -85,7 +83,7 @@ export function eventFromUnknownInput(
8583
} else {
8684
const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');
8785
const message = domException.message ? `${name}: ${domException.message}` : name;
88-
event = eventFromString(message, syntheticException, options);
86+
event = eventFromString(message, syntheticException, attachStacktrace);
8987
addExceptionTypeValue(event, message);
9088
}
9189
if ('code' in domException) {
@@ -103,7 +101,7 @@ export function eventFromUnknownInput(
103101
// it manually. This will allow us to group events based on top-level keys which is much better than creating a new
104102
// group on any key/value change.
105103
const objectException = exception as Record<string, unknown>;
106-
event = eventFromPlainObject(objectException, syntheticException, options.isRejection);
104+
event = eventFromPlainObject(objectException, syntheticException, isUnhandledRejection);
107105
addExceptionMechanism(event, {
108106
synthetic: true,
109107
});
@@ -119,7 +117,7 @@ export function eventFromUnknownInput(
119117
// - a plain Object
120118
//
121119
// So bail out and capture it as a simple message:
122-
event = eventFromString(exception as string, syntheticException, options);
120+
event = eventFromString(exception as string, syntheticException, attachStacktrace);
123121
addExceptionTypeValue(event, `${exception}`, undefined);
124122
addExceptionMechanism(event, {
125123
synthetic: true,
@@ -131,18 +129,12 @@ export function eventFromUnknownInput(
131129
/**
132130
* @hidden
133131
*/
134-
export function eventFromString(
135-
input: string,
136-
syntheticException?: Error,
137-
options: {
138-
attachStacktrace?: boolean;
139-
} = {},
140-
): Event {
132+
export function eventFromString(input: string, syntheticException?: Error, attachStacktrace?: boolean): Event {
141133
const event: Event = {
142134
message: input,
143135
};
144136

145-
if (options.attachStacktrace && syntheticException) {
137+
if (attachStacktrace && syntheticException) {
146138
const frames = parseStackFrames(syntheticException);
147139
if (frames.length) {
148140
event.stacktrace = { frames };

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ function _installGlobalOnErrorHandler(): void {
9292
error === undefined && isString(msg)
9393
? _eventFromIncompleteOnError(msg, url, line, column)
9494
: _enhanceEventWithInitialFrame(
95-
eventFromUnknownInput(error || msg, undefined, {
96-
attachStacktrace,
97-
isRejection: false,
98-
}),
95+
eventFromUnknownInput(error || msg, undefined, attachStacktrace, false),
9996
url,
10097
line,
10198
column,
@@ -145,10 +142,7 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
145142

146143
const event = isPrimitive(error)
147144
? _eventFromRejectionWithPrimitive(error)
148-
: eventFromUnknownInput(error, undefined, {
149-
attachStacktrace,
150-
isRejection: true,
151-
});
145+
: eventFromUnknownInput(error, undefined, attachStacktrace, true);
152146

153147
event.level = Severity.Error;
154148

packages/browser/src/parsers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ export function exceptionFromError(ex: Error): Exception {
3535
export function eventFromPlainObject(
3636
exception: Record<string, unknown>,
3737
syntheticException?: Error,
38-
rejection?: boolean,
38+
isUnhandledRejection?: boolean,
3939
): Event {
4040
const event: Event = {
4141
exception: {
4242
values: [
4343
{
44-
type: isEvent(exception) ? exception.constructor.name : rejection ? 'UnhandledRejection' : 'Error',
44+
type: isEvent(exception) ? exception.constructor.name : isUnhandledRejection ? 'UnhandledRejection' : 'Error',
4545
value: `Non-Error ${
46-
rejection ? 'promise rejection' : 'exception'
46+
isUnhandledRejection ? 'promise rejection' : 'exception'
4747
} captured with keys: ${extractExceptionKeysForMessage(exception)}`,
4848
},
4949
],

0 commit comments

Comments
 (0)