File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ const indentString = require('indent-string');
3
3
const cleanStack = require ( 'clean-stack' ) ;
4
4
5
5
const cleanInternalStack = stack => stack . replace ( / \s + a t .* a g g r e g a t e - e r r o r \/ i n d e x .j s : \d + : \d + \) ? / g, '' ) ;
6
+ const isString = value => typeof value === 'string' || value instanceof String ;
6
7
7
8
class AggregateError extends Error {
8
9
constructor ( errors ) {
@@ -23,7 +24,14 @@ class AggregateError extends Error {
23
24
return new Error ( error ) ;
24
25
} ) ;
25
26
26
- let message = errors . map ( error => cleanInternalStack ( cleanStack ( error . stack ) ) ) . join ( '\n' ) ;
27
+ let message = errors
28
+ . map ( error => {
29
+ // Unfortunately stack is not standardized as a property of Error instances
30
+ // which makes it necessary to explicitly check for it and its type.
31
+ // In case the stack property is missing the stringified error should be used instead.
32
+ return isString ( error . stack ) ? cleanInternalStack ( cleanStack ( error . stack ) ) : String ( error ) ;
33
+ } )
34
+ . join ( '\n' ) ;
27
35
message = '\n' + indentString ( message , 4 ) ;
28
36
super ( message ) ;
29
37
Original file line number Diff line number Diff line change @@ -26,3 +26,28 @@ test('main', t => {
26
26
Object . assign ( new Error ( ) , { code : 'EQUX' } )
27
27
] ) ;
28
28
} ) ;
29
+
30
+ test ( 'gracefully handle Error instances without a stack' , t => {
31
+ class StacklessError extends Error {
32
+ constructor ( ...args ) {
33
+ super ( ...args ) ;
34
+ this . name = this . constructor . name ;
35
+ delete this . stack ;
36
+ }
37
+ }
38
+
39
+ const error = new AggregateError ( [
40
+ new Error ( 'foo' ) ,
41
+ new StacklessError ( 'stackless' )
42
+ ] ) ;
43
+
44
+ console . log ( error ) ;
45
+
46
+ t . regex ( error . message , / E r r o r : f o o \n { 8 } a t / ) ;
47
+ t . regex ( error . message , / S t a c k l e s s E r r o r : s t a c k l e s s / ) ;
48
+
49
+ t . deepEqual ( [ ...error ] , [
50
+ new Error ( 'foo' ) ,
51
+ new StacklessError ( 'stackless' )
52
+ ] ) ;
53
+ } ) ;
You can’t perform that action at this time.
0 commit comments