import { Event , EventProcessor , Hub , Integration } from '@sentry/types' ; import { getGlobalObject , logger } from '@sentry/utils' ; const angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.*?)\n?(\S+)$/ ; /**
 * AngularJS integration
 *
 * Provides an $exceptionHandler for AngularJS
 */ export class Angular implements Integration { inheritDoc public static id : string = 'AngularJS' ; /**
     * moduleName used in Angular's DI resolution algorithm
     */ public static moduleName : string = 'ngSentry' ; inheritDoc public name : string = Angular . id ; /**
     * Angular's instance
     */ private readonly _angular : any ; /**
     * ngSentry module instance
     */ private readonly _module : any ; /**
     * Returns current hub.
     */ private _getCurrentHub ? : ( ) => Hub ; inheritDoc public constructor ( options = { } ) { logger . log ( 'You are still using the Angular integration, consider moving to @sentry/angular' ) ; this . _angular = options . angular || getGlobalObject < any > ( ) . angular ; if ( ! this . _angular ) { logger . error ( 'AngularIntegration is missing an Angular instance' ) ; return ; } this . _module = this . _angular . module ( Angular . moduleName , [ ] ) ; } inheritDoc public setupOnce ( _ , getCurrentHub ) { if ( ! this . _module ) { return ; } this . _getCurrentHub = getCurrentHub ; this . _module . config ( [ '$provide' , ( $provide ) : void => { $provide . decorator ( '$exceptionHandler' , [ '$delegate' , this . _$exceptionHandlerDecorator . bind ( this ) ] ) ; } , ] ) ; } /**
     * Angular's exceptionHandler for Sentry integration
     */ private _$exceptionHandlerDecorator ( $delegate ) { return ( exception , cause ? ) : void => { const hub = this . _getCurrentHub && this . _getCurrentHub ( ) ; if ( hub && hub . getIntegration ( Angular ) ) { hub . withScope ( scope => { if ( cause ) { scope . setExtra ( 'cause' , cause ) ; } scope . addEventProcessor ( ( event ) => { const ex = event . exception && event . exception . values && event . exception . values [ 0 ] ; if ( ex ) { const matches = angularPattern . exec ( ex . value || '' ) ; if ( matches ) { ex . type = matches [ 1 ] ; ex . value = matches [ 2 ] ; event . message = `${ ex . type }: ${ ex . value }` ; event . extra = { ... event . extra , angularDocs : matches [ 3 ] . substr ( 0 , 250 ) , } ; } } return event ; } ) ; hub . captureException ( exception ) ; } ) ; } $delegate ( exception , cause ) ; } ; } }