import { getCurrentHub , Scope } from '@sentry/core' ; import { Integration } from '@sentry/types' ; import { consoleSandbox } from '@sentry/utils' ; import { logAndExitProcess } from './utils/errorhandling' ; type UnhandledRejectionMode = 'none' | 'warn' | 'strict' ; /** Global Promise Rejection handler */ export class OnUnhandledRejection implements Integration { inheritDoc public static id : string = 'OnUnhandledRejection' ; inheritDoc public name : string = OnUnhandledRejection . id ; inheritDoc public constructor ( private readonly _options = { mode : 'warn' } ) { } inheritDoc public setupOnce ( ) { global . process . on ( 'unhandledRejection' , this . sendUnhandledPromise . bind ( this ) ) ; } param reason param promise public sendUnhandledPromise ( reason , promise ) { const hub = getCurrentHub ( ) ; if ( ! hub . getIntegration ( OnUnhandledRejection ) ) { this . _handleRejection ( reason ) ; return ; } const context = ( promise . domain && promise . domain . sentryContext ) || { } ; hub . withScope ( ( scope ) => { scope . setExtra ( 'unhandledPromiseRejection' , true ) ; if ( context . user ) { scope . setUser ( context . user ) ; } if ( context . tags ) { scope . setTags ( context . tags ) ; } if ( context . extra ) { scope . setExtras ( context . extra ) ; } hub . captureException ( reason , { originalException : promise , data : { mechanism : { handled : false , type : 'onunhandledrejection' } } , } ) ; } ) ; this . _handleRejection ( reason ) ; } /**
     * Handler for `mode` option
     */ private _handleRejection ( reason ) { const rejectionWarning = 'This error originated either by ' + 'throwing inside of an async function without a catch block, ' + 'or by rejecting a promise which was not handled with .catch().' + ' The promise rejected with the reason:' ; if ( this . _options . mode === 'warn' ) { consoleSandbox ( ( ) => { console . warn ( rejectionWarning ) ; console . error ( reason && reason . stack ? reason . stack : reason ) ; } ) ; } else if ( this . _options . mode === 'strict' ) { consoleSandbox ( ( ) => { console . warn ( rejectionWarning ) ; } ) ; logAndExitProcess ( reason ) ; } } }