Skip to content

feat(v8/browser): Remove _eventFromIncompleteOnError usage #10553

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
Feb 8, 2024
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
2 changes: 1 addition & 1 deletion packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ export { linkedErrorsIntegration } from './integrations/linkederrors';
export { browserApiErrorsIntegration } from './integrations/trycatch';

// eslint-disable-next-line deprecation/deprecation
export { GlobalHandlers, TryCatch, Breadcrumbs, LinkedErrors, HttpContext, Dedupe } from './integrations';
export { TryCatch, Breadcrumbs, LinkedErrors, HttpContext, Dedupe } from './integrations';
86 changes: 14 additions & 72 deletions packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { captureEvent, convertIntegrationFnToClass, defineIntegration, getClient } from '@sentry/core';
import type {
Client,
Event,
Integration,
IntegrationClass,
IntegrationFn,
Primitive,
StackParser,
} from '@sentry/types';
import { captureEvent, defineIntegration, getClient } from '@sentry/core';
import type { Client, Event, IntegrationFn, Primitive, StackParser } from '@sentry/types';
import {
addGlobalErrorInstrumentationHandler,
addGlobalUnhandledRejectionInstrumentationHandler,
getLocationHref,
isErrorEvent,
isPrimitive,
isString,
logger,
Expand Down Expand Up @@ -57,18 +47,6 @@ const _globalHandlersIntegration = ((options: Partial<GlobalHandlersIntegrations

export const globalHandlersIntegration = defineIntegration(_globalHandlersIntegration);

/**
* Global handlers.
* @deprecated Use `globalHandlersIntegration()` instead.
*/
// eslint-disable-next-line deprecation/deprecation
export const GlobalHandlers = convertIntegrationFnToClass(
INTEGRATION_NAME,
globalHandlersIntegration,
) as IntegrationClass<Integration & { setup: (client: Client) => void }> & {
new (options?: Partial<GlobalHandlersIntegrations>): Integration;
};

function _installGlobalOnErrorHandler(client: Client): void {
addGlobalErrorInstrumentationHandler(data => {
const { stackParser, attachStacktrace } = getOptions();
Expand All @@ -79,15 +57,12 @@ function _installGlobalOnErrorHandler(client: Client): void {

const { msg, url, line, column, error } = data;

const event =
error === undefined && isString(msg)
? _eventFromIncompleteOnError(msg, url, line, column)
: _enhanceEventWithInitialFrame(
eventFromUnknownInput(stackParser, error || msg, undefined, attachStacktrace, false),
url,
line,
column,
);
const event = _enhanceEventWithInitialFrame(
eventFromUnknownInput(stackParser, error || msg, undefined, attachStacktrace, false),
url,
line,
column,
);

event.level = 'error';

Expand Down Expand Up @@ -132,24 +107,23 @@ function _getUnhandledRejectionError(error: unknown): unknown {
return error;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const e = error as any;

// dig the object of the rejection out of known event types
try {
type ErrorWithReason = { reason: unknown };
// PromiseRejectionEvents store the object of the rejection under 'reason'
// see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
if ('reason' in e) {
return e.reason;
if ('reason' in (error as ErrorWithReason)) {
return (error as ErrorWithReason).reason;
}

type CustomEventWithDetail = { detail: { reason: unknown } };
// something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
// to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
// the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
// see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
// https://github.com/getsentry/sentry-javascript/issues/2380
else if ('detail' in e && 'reason' in e.detail) {
return e.detail.reason;
if ('detail' in (error as CustomEventWithDetail) && 'reason' in (error as CustomEventWithDetail).detail) {
return (error as CustomEventWithDetail).detail.reason;
}
} catch {} // eslint-disable-line no-empty

Expand All @@ -176,38 +150,6 @@ function _eventFromRejectionWithPrimitive(reason: Primitive): Event {
};
}

/**
* This function creates a stack from an old, error-less onerror handler.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _eventFromIncompleteOnError(msg: any, url: any, line: any, column: any): Event {
const ERROR_TYPES_RE =
/^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$/i;

// If 'message' is ErrorEvent, get real message from inside
let message = isErrorEvent(msg) ? msg.message : msg;
let name = 'Error';

const groups = message.match(ERROR_TYPES_RE);
if (groups) {
name = groups[1];
message = groups[2];
}

const event = {
exception: {
values: [
{
type: name,
value: message,
},
],
},
};

return _enhanceEventWithInitialFrame(event, url, line, column);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _enhanceEventWithInitialFrame(event: Event, url: any, line: any, column: any): Event {
// event.exception
Expand Down
1 change: 0 additions & 1 deletion packages/browser/src/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable deprecation/deprecation */
export { GlobalHandlers } from './globalhandlers';
export { TryCatch } from './trycatch';
export { Breadcrumbs } from './breadcrumbs';
export { LinkedErrors } from './linkederrors';
Expand Down