@@ -92,4 +92,54 @@ describe('stackWithCauses()', () => {
92
92
const result = stackWithCauses ( err ) ;
93
93
result . should . equal ( 'xyz789\ncaused by: abc123\ncaused by: xyz789\ncauses have become circular...' ) ;
94
94
} ) ;
95
+
96
+ describe ( 'should append non-Error causes to the end of the cause trail' , ( ) => {
97
+ it ( 'for string causes' , ( ) => {
98
+ const err = new ErrorWithCause ( 'foo' , { cause : 'string cause' } ) ;
99
+ err . stack = 'xyz789' ;
100
+ stackWithCauses ( err ) . should . equal ( 'xyz789\ncaused by: "string cause"' ) ;
101
+ } ) ;
102
+
103
+ it ( 'for number causes' , ( ) => {
104
+ const err = new ErrorWithCause ( 'foo' , { cause : 123 } ) ;
105
+ err . stack = 'xyz789' ;
106
+ stackWithCauses ( err ) . should . equal ( 'xyz789\ncaused by: 123' ) ;
107
+ } ) ;
108
+
109
+ it ( 'for non-Error object causes' , ( ) => {
110
+ const err = new ErrorWithCause ( 'foo' , { cause : { name : 'TypeError' , message : 'foo' } } ) ;
111
+ err . stack = 'xyz789' ;
112
+ stackWithCauses ( err ) . should . equal ( 'xyz789\ncaused by: {"name":"TypeError","message":"foo"}' ) ;
113
+ } ) ;
114
+
115
+ it ( 'should not throw for circular non-Error object causes' , ( ) => {
116
+ const firstCause = { first : true } ;
117
+ const secondCause = { second : true , firstCause } ;
118
+
119
+ // @ts -ignore
120
+ firstCause . secondCause = secondCause ;
121
+
122
+ const err = new ErrorWithCause ( 'foo' , { cause : firstCause } ) ;
123
+ err . stack = 'xyz789' ;
124
+ stackWithCauses ( err ) . should . equal ( 'xyz789\ncaused by: <failed to stringify value>' ) ;
125
+ } ) ;
126
+
127
+ // Copied from https://github.com/nodejs/node/blob/5e6f9c3e346b196ab299a3fce485d7aa5fbf3802/test/parallel/test-util-inspect.js#L663-L677
128
+ it ( 'for falsy causes' , ( ) => {
129
+ const falsyCause1 = new ErrorWithCause ( '' , { cause : false } ) ;
130
+ delete falsyCause1 . stack ;
131
+
132
+ // @ts -ignore
133
+ // eslint-disable-next-line unicorn/no-null
134
+ const falsyCause2 = new ErrorWithCause ( undefined , { cause : null } ) ;
135
+ falsyCause2 . stack = '' ;
136
+
137
+ const undefinedCause = new ErrorWithCause ( '' , { cause : undefined } ) ;
138
+ undefinedCause . stack = '' ;
139
+
140
+ stackWithCauses ( falsyCause1 ) . should . equal ( '\ncaused by: false' ) ;
141
+ stackWithCauses ( falsyCause2 ) . should . equal ( '\ncaused by: null' ) ;
142
+ stackWithCauses ( undefinedCause ) . should . equal ( '\ncaused by: undefined' ) ;
143
+ } ) ;
144
+ } ) ;
95
145
} ) ;
0 commit comments