Skip to content

ref(browser): Make attachStacktrace usage more explicit #4628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/browser/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
* @inheritDoc
*/
public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> {
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<Event> {
return eventFromMessage(this._options, message, level, hint);
return eventFromMessage(message, level, hint, this._options.attachStacktrace);
}

/**
Expand Down
40 changes: 16 additions & 24 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event, EventHint, Options, Severity } from '@sentry/types';
import { Event, EventHint, Severity } from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
Expand All @@ -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<Event> {
export function eventFromException(
exception: unknown,
hint?: EventHint,
attachStacktrace?: boolean,
): PromiseLike<Event> {
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) {
Expand All @@ -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?: boolean,
): PromiseLike<Event> {
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;
Expand All @@ -57,10 +57,8 @@ export function eventFromMessage(
export function eventFromUnknownInput(
exception: unknown,
syntheticException?: Error,
options: {
isRejection?: boolean;
attachStacktrace?: boolean;
} = {},
attachStacktrace?: boolean,
isUnhandledRejection?: boolean,
): Event {
let event: Event;

Expand All @@ -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) {
Expand All @@ -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<string, unknown>;
event = eventFromPlainObject(objectException, syntheticException, options.isRejection);
event = eventFromPlainObject(objectException, syntheticException, isUnhandledRejection);
addExceptionMechanism(event, {
synthetic: true,
});
Expand All @@ -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,
Expand All @@ -131,18 +129,12 @@ export function eventFromUnknownInput(
/**
* @hidden
*/
export function eventFromString(
input: string,
syntheticException?: Error,
options: {
attachStacktrace?: boolean;
} = {},
): Event {
export function eventFromString(input: string, syntheticException?: Error, attachStacktrace?: boolean): Event {
const event: Event = {
message: input,
};

if (options.attachStacktrace && syntheticException) {
if (attachStacktrace && syntheticException) {
const frames = parseStackFrames(syntheticException);
if (frames.length) {
event.stacktrace = { frames };
Expand Down
10 changes: 2 additions & 8 deletions packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions packages/browser/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export function exceptionFromError(ex: Error): Exception {
export function eventFromPlainObject(
exception: Record<string, unknown>,
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)}`,
},
],
Expand Down