-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(utils): Refactor addInstrumentationHandler
to dedicated methods
#9542
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
Changes from all commits
81399fc
d53401b
1071982
6b2f328
3f07f3c
279671c
8f368db
be2f51e
7cc1856
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
import { getCurrentHub } from '@sentry/core'; | ||
import type { Event, Hub, Integration, Primitive, StackParser } from '@sentry/types'; | ||
import { addInstrumentationHandler, getLocationHref, isErrorEvent, isPrimitive, isString, logger } from '@sentry/utils'; | ||
import { | ||
addGlobalErrorInstrumentationHandler, | ||
addGlobalUnhandledRejectionInstrumentationHandler, | ||
getLocationHref, | ||
isErrorEvent, | ||
isPrimitive, | ||
isString, | ||
logger, | ||
} from '@sentry/utils'; | ||
|
||
import type { BrowserClient } from '../client'; | ||
import { eventFromUnknownInput } from '../eventbuilder'; | ||
|
@@ -68,96 +76,97 @@ export class GlobalHandlers implements Integration { | |
} | ||
} | ||
|
||
/** JSDoc */ | ||
function _installGlobalOnErrorHandler(): void { | ||
addInstrumentationHandler( | ||
'error', | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(data: { msg: any; url: any; line: any; column: any; error: any }) => { | ||
const [hub, stackParser, attachStacktrace] = getHubAndOptions(); | ||
if (!hub.getIntegration(GlobalHandlers)) { | ||
return; | ||
} | ||
const { msg, url, line, column, error } = data; | ||
if (shouldIgnoreOnError() || (error && error.__sentry_own_request__)) { | ||
return; | ||
} | ||
addGlobalErrorInstrumentationHandler(data => { | ||
const [hub, stackParser, attachStacktrace] = getHubAndOptions(); | ||
if (!hub.getIntegration(GlobalHandlers)) { | ||
return; | ||
} | ||
const { msg, url, line, column, error } = data; | ||
if (shouldIgnoreOnError()) { | ||
return; | ||
} | ||
|
||
const event = | ||
error === undefined && isString(msg) | ||
? _eventFromIncompleteOnError(msg, url, line, column) | ||
: _enhanceEventWithInitialFrame( | ||
eventFromUnknownInput(stackParser, error || msg, undefined, attachStacktrace, false), | ||
url, | ||
line, | ||
column, | ||
); | ||
|
||
event.level = 'error'; | ||
|
||
hub.captureEvent(event, { | ||
originalException: error, | ||
mechanism: { | ||
handled: false, | ||
type: 'onerror', | ||
}, | ||
}); | ||
}, | ||
); | ||
const event = | ||
error === undefined && isString(msg) | ||
? _eventFromIncompleteOnError(msg, url, line, column) | ||
: _enhanceEventWithInitialFrame( | ||
eventFromUnknownInput(stackParser, error || msg, undefined, attachStacktrace, false), | ||
url, | ||
line, | ||
column, | ||
); | ||
|
||
event.level = 'error'; | ||
|
||
hub.captureEvent(event, { | ||
originalException: error, | ||
mechanism: { | ||
handled: false, | ||
type: 'onerror', | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
/** JSDoc */ | ||
function _installGlobalOnUnhandledRejectionHandler(): void { | ||
addInstrumentationHandler( | ||
'unhandledrejection', | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(e: any) => { | ||
const [hub, stackParser, attachStacktrace] = getHubAndOptions(); | ||
if (!hub.getIntegration(GlobalHandlers)) { | ||
return; | ||
} | ||
let error = e; | ||
|
||
// dig the object of the rejection out of known event types | ||
try { | ||
// PromiseRejectionEvents store the object of the rejection under 'reason' | ||
// see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent | ||
if ('reason' in e) { | ||
error = e.reason; | ||
} | ||
// 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) { | ||
error = e.detail.reason; | ||
} | ||
} catch (_oO) { | ||
// no-empty | ||
} | ||
addGlobalUnhandledRejectionInstrumentationHandler(e => { | ||
const [hub, stackParser, attachStacktrace] = getHubAndOptions(); | ||
if (!hub.getIntegration(GlobalHandlers)) { | ||
return; | ||
} | ||
|
||
if (shouldIgnoreOnError() || (error && error.__sentry_own_request__)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note here: |
||
return true; | ||
} | ||
if (shouldIgnoreOnError()) { | ||
return true; | ||
} | ||
|
||
const event = isPrimitive(error) | ||
? _eventFromRejectionWithPrimitive(error) | ||
: eventFromUnknownInput(stackParser, error, undefined, attachStacktrace, true); | ||
const error = _getUnhandledRejectionError(e as unknown); | ||
|
||
event.level = 'error'; | ||
const event = isPrimitive(error) | ||
? _eventFromRejectionWithPrimitive(error) | ||
: eventFromUnknownInput(stackParser, error, undefined, attachStacktrace, true); | ||
|
||
hub.captureEvent(event, { | ||
originalException: error, | ||
mechanism: { | ||
handled: false, | ||
type: 'onunhandledrejection', | ||
}, | ||
}); | ||
event.level = 'error'; | ||
|
||
return; | ||
}, | ||
); | ||
hub.captureEvent(event, { | ||
originalException: error, | ||
mechanism: { | ||
handled: false, | ||
type: 'onunhandledrejection', | ||
}, | ||
}); | ||
|
||
return; | ||
}); | ||
} | ||
|
||
function _getUnhandledRejectionError(error: unknown): unknown { | ||
if (isPrimitive(error)) { | ||
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 { | ||
// 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; | ||
} | ||
|
||
// 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; | ||
} | ||
} catch {} // eslint-disable-line no-empty | ||
|
||
return error; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,12 @@ import { | |
Integrations as CoreIntegrations, | ||
} from '@sentry/core'; | ||
import type { UserFeedback } from '@sentry/types'; | ||
import { addInstrumentationHandler, logger, stackParserFromStackParserOptions, supportsFetch } from '@sentry/utils'; | ||
import { | ||
addHistoryInstrumentationHandler, | ||
logger, | ||
stackParserFromStackParserOptions, | ||
supportsFetch, | ||
} from '@sentry/utils'; | ||
|
||
import type { BrowserClientOptions, BrowserOptions } from './client'; | ||
import { BrowserClient } from './client'; | ||
|
@@ -240,9 +245,9 @@ function startSessionTracking(): void { | |
startSessionOnHub(hub); | ||
|
||
// We want to create a session for every navigation as well | ||
addInstrumentationHandler('history', ({ from, to }) => { | ||
addHistoryInstrumentationHandler(({ from, to }) => { | ||
// Don't create an additional session for the initial route or if the location did not change | ||
if (!(from === undefined || from === to)) { | ||
if (from !== undefined && from !== to) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this condition is a bit easier to read IMHO |
||
startSessionOnHub(getCurrentHub()); | ||
} | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was actually incorrect because
from
can technically beundefined
.