@@ -66,13 +66,20 @@ describe('WebExceptionInstrumentation', () => {
6666 source ?: string ,
6767 lineno ?: number ,
6868 colno ?: number ,
69- error ?: Error
69+ error ?: Error | string
7070 ) => {
71- if ( error ?. name !== 'ValidationError' ) {
71+ console . log ( error ) ;
72+ if ( error instanceof Error && error . name !== 'ValidationError' ) {
7273 // If we are testing our instrumentation, we want to let the error propagate.
7374 // If it is any other kind of error, we want Mocha to handle the error as expected.
7475 mochaErrorHandler ?. call ( window , event , source , lineno , colno , error ) ;
7576 }
77+
78+ if ( typeof error === 'string' && error !== 'string' ) {
79+ // If we are testing our instrumentation, we want to let the error propagate.
80+ // If it is any other kind of error, we want Mocha to handle the error as expected.
81+ mochaErrorHandler ?. call ( window , event , source , lineno , colno ) ;
82+ }
7683 } ;
7784 } ) ;
7885
@@ -140,10 +147,27 @@ describe('WebExceptionInstrumentation', () => {
140147 assert . strictEqual ( body [ ATTR_EXCEPTION_STACKTRACE ] , stack ) ;
141148 } , 0 ) ;
142149 } ) ;
150+
151+ it ( 'should handle throwing an error as a string' , async ( ) => {
152+ setTimeout ( ( ) => {
153+ throw 'string' ;
154+ } ) ;
155+
156+ setTimeout ( ( ) => {
157+ const events = exporter . getFinishedLogRecords ( ) ;
158+ // assert.ok(events.length > 0, 'Expected at least one log record');
159+ const event = events [ 0 ] ;
160+ const body = event . body as Record < string , any > ;
161+ assert . strictEqual ( body [ ATTR_EXCEPTION_MESSAGE ] , 'string' ) ;
162+ } , 0 ) ;
163+ } ) ;
143164 } ) ;
144165
145166 describe ( 'adding custom attributes' , ( ) => {
146- const applyCustomAttrs = ( error : Error ) => {
167+ const applyCustomAttrs = ( error : Error | string ) => {
168+ if ( typeof error === 'string' ) {
169+ return { 'app.custom.exception' : error . toLocaleUpperCase ( ) } ;
170+ }
147171 return {
148172 'app.custom.exception' : error . message . toLocaleUpperCase ( ) ,
149173 } ;
@@ -178,5 +202,20 @@ describe('WebExceptionInstrumentation', () => {
178202 ) ;
179203 } , 0 ) ;
180204 } ) ;
205+
206+ it ( 'should add custom attributes if the error is a string' , async ( ) => {
207+ setTimeout ( ( ) => {
208+ throw 'string' ;
209+ } ) ;
210+
211+ setTimeout ( ( ) => {
212+ const events = exporter . getFinishedLogRecords ( ) ;
213+ assert . ok ( events . length > 0 , 'Expected at least one log record' ) ;
214+ const event = events [ 0 ] ;
215+ const body = event . body as Record < string , any > ;
216+ assert . strictEqual ( body [ ATTR_EXCEPTION_MESSAGE ] , 'string' ) ;
217+ assert . strictEqual ( event . attributes [ 'app.custom.exception' ] , 'STRING' ) ;
218+ } , 0 ) ;
219+ } ) ;
181220 } ) ;
182221} ) ;
0 commit comments