Skip to content

Commit ad8931c

Browse files
committed
fix(zone.js): classes that extend Error should retain cause property (angular#61599)
ZoneAwareError previously did not copy the cause property over to the `this` object when an error extends the native error class. PR Close angular#61599
1 parent 5735ea7 commit ad8931c

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

packages/zone.js/lib/common/error-rewrite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export function patchError(Zone: ZoneType): void {
143143
// We got called with a `new` operator AND we are subclass of ZoneAwareError
144144
// in that case we have to copy all of our properties to `this`.
145145
Object.keys(error)
146-
.concat('stack', 'message')
146+
.concat('stack', 'message', 'cause')
147147
.forEach((key) => {
148148
const value = (error as any)[key];
149149
if (value !== undefined) {

packages/zone.js/test/common/Error.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@ describe('ZoneAwareError', () => {
180180
expect(spy).toHaveBeenCalledWith('test');
181181
});
182182

183+
it('should copy cause on error that extends native', () => {
184+
class WrappedError extends Error {
185+
constructor(error: unknown) {
186+
super(
187+
'wrapped',
188+
// @ts-ignore
189+
{cause: error},
190+
);
191+
}
192+
}
193+
const cause = new Error('original');
194+
const wrapped = new WrappedError(cause) as any;
195+
expect(wrapped.cause).toBe(cause);
196+
});
197+
183198
it('should always have stack property even without throw', () => {
184199
// in IE, the stack will be undefined without throw
185200
// in ZoneAwareError, we will make stack always be

0 commit comments

Comments
 (0)