@@ -307,6 +307,71 @@ describe('Class: PowertoolLogFormatter', () => {
307
307
308
308
expect ( shouldThrow ) . toThrowError ( expect . any ( URIError ) ) ;
309
309
} ) ;
310
+
311
+ test ( 'when an error with cause of type Error is formatted, the cause key is included and formatted' , ( ) => {
312
+ // Prepare
313
+ const formatter = new PowertoolLogFormatter ( ) ;
314
+ class ErrorWithCause extends Error {
315
+ public cause ?: Error ;
316
+ public constructor ( message : string , options ?: { cause : Error } ) {
317
+ super ( message ) ;
318
+ this . cause = options ?. cause ;
319
+ }
320
+ }
321
+
322
+ // Act
323
+ const formattedURIError = formatter . formatError (
324
+ new ErrorWithCause ( 'foo' , { cause : new Error ( 'bar' ) } )
325
+ ) ;
326
+
327
+ // Assess
328
+ expect ( formattedURIError ) . toEqual ( {
329
+ location : expect . stringMatching ( / P o w e r t o o l L o g F o r m a t t e r .t e s t .t s : [ 0 - 9 ] + / ) ,
330
+ message : 'foo' ,
331
+ name : 'Error' ,
332
+ stack : expect . stringMatching (
333
+ / P o w e r t o o l L o g F o r m a t t e r .t e s t .t s : [ 0 - 9 ] + : [ 0 - 9 ] + /
334
+ ) ,
335
+ cause : {
336
+ location : expect . stringMatching (
337
+ / P o w e r t o o l L o g F o r m a t t e r .t e s t .t s : [ 0 - 9 ] + /
338
+ ) ,
339
+ message : 'bar' ,
340
+ name : 'Error' ,
341
+ stack : expect . stringMatching (
342
+ / P o w e r t o o l L o g F o r m a t t e r .t e s t .t s : [ 0 - 9 ] + : [ 0 - 9 ] + /
343
+ ) ,
344
+ } ,
345
+ } ) ;
346
+ } ) ;
347
+
348
+ test ( 'when an error with cause of type other than Error is formatted, the cause key is included as-is' , ( ) => {
349
+ // Prepare
350
+ const formatter = new PowertoolLogFormatter ( ) ;
351
+ class ErrorWithCause extends Error {
352
+ public cause ?: unknown ;
353
+ public constructor ( message : string , options ?: { cause : unknown } ) {
354
+ super ( message ) ;
355
+ this . cause = options ?. cause ;
356
+ }
357
+ }
358
+
359
+ // Act
360
+ const formattedURIError = formatter . formatError (
361
+ new ErrorWithCause ( 'foo' , { cause : 'bar' } )
362
+ ) ;
363
+
364
+ // Assess
365
+ expect ( formattedURIError ) . toEqual ( {
366
+ location : expect . stringMatching ( / P o w e r t o o l L o g F o r m a t t e r .t e s t .t s : [ 0 - 9 ] + / ) ,
367
+ message : 'foo' ,
368
+ name : 'Error' ,
369
+ stack : expect . stringMatching (
370
+ / P o w e r t o o l L o g F o r m a t t e r .t e s t .t s : [ 0 - 9 ] + : [ 0 - 9 ] + /
371
+ ) ,
372
+ cause : 'bar' ,
373
+ } ) ;
374
+ } ) ;
310
375
} ) ;
311
376
312
377
describe ( 'Method: formatTimestamp' , ( ) => {
0 commit comments