import { ExceptionlessClient , IEventPlugin , PluginContext } from "@exceptionless/core" ; declare let $ ; export class BrowserGlobalHandlerPlugin implements IEventPlugin { public priority : number = 100 ; public name : string = "BrowserGlobalHandlerPlugin" ; private _client : ExceptionlessClient | null = null ; public startup ( context ) { if ( this . _client || typeof window !== "object" ) { return Promise . resolve ( ) ; } this . _client = context . client ; window . addEventListener ( "error" , event => { void this . _client ?. submitUnhandledException ( this . getError ( event ) , "onerror" ) ; } ) ; window . addEventListener ( "unhandledrejection" , event => { let error = event . reason ; try { const reason = ( < { detail ? : { reason : string ; } ; } > event ) . detail ?. reason ; if ( reason ) { error = reason ; } } catch ( ex ) { } void this . _client ?. submitUnhandledException ( error , "onunhandledrejection" ) ; } ) ; if ( typeof $ !== "undefined" && $ ( document ) ) { $ ( document ) . ajaxError ( ( _ , xhr , settings , error ) => { if ( xhr . status === 404 ) { void this . _client ?. submitNotFound ( settings . url ) ; } else if ( xhr . status !== 401 ) { void this . _client ?. createUnhandledException ( new Error ( error ) , "JQuery.ajaxError" ) . setSource ( settings . url ) . setProperty ( "status" , xhr . status ) . setProperty ( "request" , settings . data ) . setProperty ( "response" , xhr . responseText ?. slice ( 0 , 1024 ) ) . submit ( ) ; } } ) ; } return Promise . resolve ( ) ; } private getError ( event ) { const { error , message , filename , lineno , colno } = event ; if ( typeof error === "object" ) { return error as Error ; } let name = "Error" ; let msg = message || event . error ; if ( msg ) { const errorNameRegex = /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Aggregate|Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$/i ; const regexResult = errorNameRegex . exec ( msg ) ; if ( regexResult ) { const [ , errorName , errorMessage ] = regexResult ; if ( errorName ) { name = errorName ; } if ( errorMessage ) { msg = errorMessage ; } } } const ex = new Error ( msg || "Script error." ) ; ex . name = name ; ex . stack = `at ${ filename || "" }:${ ! isNaN ( lineno ) ? lineno : 0 }${ ! isNaN ( colno ) ? `:${ colno }` : "" }` ; return ex ; } }