|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import config from '@automattic/calypso-config'; |
|
|
import type * as SentryApi from '@sentry/react'; |
|
|
|
|
|
|
|
|
const SENTRY_CONFIG: SentryApi.BrowserOptions = { |
|
|
dsn: 'https://61275d63a504465ab315245f1a379dab@o248881.ingest.sentry.io/6313676', |
|
|
|
|
|
sampleRate: 1.0, |
|
|
|
|
|
denyUrls: [ |
|
|
|
|
|
/^[a-z]+(-[a-z]+)?-extension:\/\//i, |
|
|
], |
|
|
}; |
|
|
|
|
|
type SupportedMethods = |
|
|
| 'addBreadcrumb' |
|
|
| 'captureEvent' |
|
|
| 'captureException' |
|
|
| 'captureMessage' |
|
|
| 'configureScope' |
|
|
| 'withScope'; |
|
|
interface QueueDataMethod< Method extends SupportedMethods > { |
|
|
f: Method; |
|
|
a: Parameters< ( typeof SentryApi )[ Method ] >; |
|
|
} |
|
|
|
|
|
let callQueue: Array< Readonly< QueueDataMethod< SupportedMethods > > > = []; |
|
|
let errorQueue: Array< Readonly< Parameters< OnErrorEventHandlerNonNull > > > = []; |
|
|
let rejectionQueue: Array< PromiseRejectionEvent > = []; |
|
|
function clearQueues() { |
|
|
callQueue = []; |
|
|
errorQueue = []; |
|
|
rejectionQueue = []; |
|
|
} |
|
|
|
|
|
type SentryState = |
|
|
|
|
|
| { state: 'initial' } |
|
|
|
|
|
| { state: 'loading'; params: SentryOptions } |
|
|
|
|
|
| { state: 'error'; params: SentryOptions } |
|
|
|
|
|
| { state: 'disabled'; params: SentryOptions } |
|
|
|
|
|
| { state: 'loaded'; params: SentryOptions; sentry: typeof import('@sentry/react') }; |
|
|
let state: SentryState = { state: 'initial' }; |
|
|
|
|
|
function dispatchSentryMethodCall< Method extends SupportedMethods >( |
|
|
method: Method, |
|
|
args: Parameters< ( typeof SentryApi )[ Method ] > |
|
|
) { |
|
|
const { state: status } = state; |
|
|
if ( status === 'loaded' ) { |
|
|
|
|
|
state.sentry[ method ]( ...args ); |
|
|
return; |
|
|
} |
|
|
if ( status === 'error' || status === 'disabled' ) { |
|
|
return; |
|
|
} |
|
|
callQueue.push( { f: method, a: args } ); |
|
|
} |
|
|
export function addBreadcrumb( ...args: Parameters< typeof SentryApi.addBreadcrumb > ) { |
|
|
dispatchSentryMethodCall( 'addBreadcrumb', args ); |
|
|
} |
|
|
export function captureEvent( ...args: Parameters< typeof SentryApi.captureEvent > ) { |
|
|
dispatchSentryMethodCall( 'captureEvent', args ); |
|
|
} |
|
|
export function captureException( ...args: Parameters< typeof SentryApi.captureException > ) { |
|
|
dispatchSentryMethodCall( 'captureException', args ); |
|
|
} |
|
|
export function captureMessage( ...args: Parameters< typeof SentryApi.captureMessage > ) { |
|
|
dispatchSentryMethodCall( 'captureMessage', args ); |
|
|
} |
|
|
export function configureScope( ...args: Parameters< typeof SentryApi.configureScope > ) { |
|
|
dispatchSentryMethodCall( 'configureScope', args ); |
|
|
} |
|
|
export function withScope( ...args: Parameters< typeof SentryApi.withScope > ) { |
|
|
dispatchSentryMethodCall( 'withScope', args ); |
|
|
} |
|
|
|
|
|
|
|
|
function processErrorQueue() { |
|
|
for ( const call of callQueue ) { |
|
|
|
|
|
state.sentry[ call.f ]( ...call.a ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( window.onerror ) { |
|
|
for ( const error of errorQueue ) { |
|
|
window.onerror( ...error ); |
|
|
} |
|
|
} |
|
|
if ( window.onunhandledrejection ) { |
|
|
for ( const rejection of rejectionQueue ) { |
|
|
window.onunhandledrejection( rejection ); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
function beforeBreadcrumb( breadcrumb: SentryApi.Breadcrumb ): SentryApi.Breadcrumb | null { |
|
|
|
|
|
if ( breadcrumb.category === 'navigation' && ! breadcrumb.data?.should_capture ) { |
|
|
return null; |
|
|
} else if ( breadcrumb.data?.should_capture ) { |
|
|
delete breadcrumb.data.should_capture; |
|
|
} |
|
|
return breadcrumb; |
|
|
} |
|
|
|
|
|
function shouldEnableSentry( sampleRate: number = 0.1 ): boolean { |
|
|
|
|
|
if ( config.isEnabled( 'always-enable-sentry' ) ) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
return ( |
|
|
config.isEnabled( 'catch-js-errors' ) && |
|
|
Math.ceil( Math.random() * 10 ) <= Math.floor( sampleRate * 10 ) |
|
|
); |
|
|
} |
|
|
|
|
|
interface SentryOptions { |
|
|
beforeSend?: ( e: SentryApi.Event ) => SentryApi.Event | null; |
|
|
userId?: number; |
|
|
sampleRate?: number; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function initSentry( parameters?: SentryOptions ) { |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
if ( typeof document === 'undefined' || ! [ 'initial', 'disabled' ].includes( state.state ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const params = |
|
|
state.state === 'initial' ? parameters || {} : { ...state.params, ...parameters }; |
|
|
|
|
|
const { beforeSend, userId, sampleRate } = params; |
|
|
|
|
|
state = { state: 'loading', params }; |
|
|
|
|
|
|
|
|
if ( ! shouldEnableSentry( sampleRate ) ) { |
|
|
state = { state: 'disabled', params }; |
|
|
|
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
console.info( 'Initializing error reporting...' ); |
|
|
|
|
|
const errorHandler = ( errorEvent: ErrorEvent ): void => |
|
|
void errorQueue.push( [ |
|
|
errorEvent.message, |
|
|
errorEvent.filename, |
|
|
errorEvent.lineno, |
|
|
errorEvent.colno, |
|
|
errorEvent.error, |
|
|
] ); |
|
|
const rejectionHandler = ( exceptionEvent: PromiseRejectionEvent ): void => |
|
|
void rejectionQueue.push( |
|
|
|
|
|
|
|
|
|
|
|
exceptionEvent?.reason ?? exceptionEvent?.detail?.reason ?? exceptionEvent |
|
|
); |
|
|
|
|
|
window.addEventListener( 'error', errorHandler ); |
|
|
window.addEventListener( 'unhandledrejection', rejectionHandler ); |
|
|
|
|
|
try { |
|
|
const Sentry = await import( '@sentry/react' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const environment = config< string >( 'env_id' ); |
|
|
const release = |
|
|
environment === 'production' || environment === 'horizon' |
|
|
? `calypso_${ window.COMMIT_SHA }` |
|
|
: undefined; |
|
|
|
|
|
|
|
|
window.removeEventListener( 'error', errorHandler ); |
|
|
window.removeEventListener( 'unhandledrejection', rejectionHandler ); |
|
|
|
|
|
|
|
|
Sentry.init( { |
|
|
...SENTRY_CONFIG, |
|
|
initialScope: { |
|
|
user: { id: userId?.toString() }, |
|
|
}, |
|
|
environment, |
|
|
release, |
|
|
beforeBreadcrumb, |
|
|
beforeSend, |
|
|
} ); |
|
|
state = { state: 'loaded', params, sentry: Sentry }; |
|
|
} catch ( err ) { |
|
|
state = { state: 'error', params }; |
|
|
|
|
|
window.removeEventListener( 'error', errorHandler ); |
|
|
window.removeEventListener( 'unhandledrejection', rejectionHandler ); |
|
|
throw err; |
|
|
} |
|
|
|
|
|
processErrorQueue(); |
|
|
} catch ( o_O ) { |
|
|
|
|
|
console.error( o_O ); |
|
|
} finally { |
|
|
|
|
|
|
|
|
clearQueues(); |
|
|
} |
|
|
} |
|
|
|