11import {
22 ExceptionlessClient ,
33 IEventPlugin ,
4- PluginContext ,
5- nameof
4+ PluginContext
65} from "@exceptionless/core" ;
76
8- export class GlobalHandlerPlugin implements IEventPlugin {
7+ declare var $ : ( document : Document ) => { ( ) : any ; new ( ) : any ; ajaxError : { ( document : ( event : Event , xhr : { responseText : string ; status : number ; } , settings : { data : unknown ; url : string ; } , error : string ) => void ) : void ; new ( ) : any ; } ; } ;
8+
9+ export class BrowserGlobalHandlerPlugin implements IEventPlugin {
910 public priority : number = 100 ;
10- public name : string = "GlobalHandlerPlugin " ;
11+ public name : string = "BrowserGlobalHandlerPlugin " ;
1112
1213 private _client : ExceptionlessClient = null ;
1314
@@ -17,13 +18,15 @@ export class GlobalHandlerPlugin implements IEventPlugin {
1718 }
1819
1920 this . _client = context . client ;
20- Error . stackTraceLimit = Infinity ;
21+ Error . stackTraceLimit = 50 ;
2122
2223 // TODO: Discus if we want to unwire this handler in suspend?
2324 const originalOnError : OnErrorEventHandlerNonNull = globalThis . onerror ;
2425 globalThis . onerror = ( event : Event | string , source ?: string , lineno ?: number , colno ?: number , error ?: Error ) => {
2526 // TODO: Handle async
26- void this . _client . submitUnhandledException ( error || this . buildError ( event , source , lineno , colno ) , nameof < Window > ( "onerror" ) ) ;
27+ void this . _client . createUnhandledException ( error || this . buildError ( event , source , lineno , colno ) , "onerror" )
28+ . setSource ( source )
29+ . submit ( ) ;
2730
2831 // eslint-disable-next-line prefer-rest-params
2932 return originalOnError ? originalOnError . apply ( this , ...arguments ) : false ;
@@ -41,18 +44,40 @@ export class GlobalHandlerPlugin implements IEventPlugin {
4144 } catch ( ex ) { }
4245
4346 // TODO: Handle async
44- void this . _client . submitUnhandledException ( error , nameof < Window > ( "onunhandledrejection" ) ) ;
47+ void this . _client . submitUnhandledException ( error , "onunhandledrejection" ) ;
4548
4649 // eslint-disable-next-line prefer-rest-params
4750 return originalOnunhandledrejection ? originalOnunhandledrejection . apply ( this , ...arguments ) : false ;
4851 } ;
4952
53+ if ( typeof $ !== "undefined" && $ ( document ) ) {
54+ $ ( document ) . ajaxError ( ( event : Event , xhr : { responseText : string , status : number } , settings : { data : unknown , url : string } , error : string ) => {
55+ if ( xhr . status === 404 ) {
56+ // TODO: Handle async
57+ void this . _client . submitNotFound ( settings . url ) ;
58+ } else if ( xhr . status !== 401 ) {
59+ // TODO: Handle async
60+ void this . _client . createUnhandledException ( new Error ( error ) , "JQuery.ajaxError" )
61+ . setSource ( settings . url )
62+ . setProperty ( "status" , xhr . status )
63+ . setProperty ( "request" , settings . data )
64+ . setProperty ( "response" , xhr . responseText ?. slice ( 0 , 1024 ) )
65+ . submit ( ) ;
66+ }
67+ } ) ;
68+ }
69+
5070 return Promise . resolve ( ) ;
5171 }
5272
5373 private buildError ( event : Event | string , source ?: string , lineno ?: number , colno ?: number ) : Error {
74+ if ( Object . prototype . toString . call ( event ) === "[object ErrorEvent]" ) {
75+ // TODO: See if this is the error event.
76+ return ( < ErrorEvent > event ) . error ;
77+ }
78+
5479 let name : string = "Error" ;
55- let message : string = Object . prototype . toString . call ( event ) === '[object ErrorEvent]' ? ( < ErrorEvent > event ) . message : null ;
80+ let message : string = Object . prototype . toString . call ( event ) === '[object ErrorEvent]' ? ( < ErrorEvent > event ) . error : null ;
5681 if ( message ) {
5782 const errorNameRegex : RegExp = / ^ (?: [ U u ] n c a u g h t (?: e x c e p t i o n : ) ? ) ? (?: ( (?: A g g r e g a t e | E v a l | I n t e r n a l | R a n g e | R e f e r e n c e | S y n t a x | T y p e | U R I | ) E r r o r ) : ) ? ( .* ) $ ) / i;
5883 const [ _ , errorName , errorMessage ] = errorNameRegex . exec ( message ) ;
@@ -64,7 +89,7 @@ export class GlobalHandlerPlugin implements IEventPlugin {
6489 }
6590 }
6691
67- const error = new Error ( message || "Script error" ) ;
92+ const error = new Error ( message || "Script error. " ) ;
6893 error . name = name ;
6994 error . stack = `at ${ source || "" } :${ ! isNaN ( lineno ) ? lineno : 0 } ${ ! isNaN ( colno ) ? ":" + colno : "" } ` ;
7095 return error ;
0 commit comments