Skip to content

Commit 5f1bd07

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

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy