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
This commit is contained in:
Andrew Scott 2025-05-21 16:49:08 -07:00
parent 5735ea7115
commit ad8931cb49
2 changed files with 16 additions and 1 deletions

View file

@ -143,7 +143,7 @@ export function patchError(Zone: ZoneType): void {
// We got called with a `new` operator AND we are subclass of ZoneAwareError
// in that case we have to copy all of our properties to `this`.
Object.keys(error)
.concat('stack', 'message')
.concat('stack', 'message', 'cause')
.forEach((key) => {
const value = (error as any)[key];
if (value !== undefined) {

View file

@ -180,6 +180,21 @@ describe('ZoneAwareError', () => {
expect(spy).toHaveBeenCalledWith('test');
});
it('should copy cause on error that extends native', () => {
class WrappedError extends Error {
constructor(error: unknown) {
super(
'wrapped',
// @ts-ignore
{cause: error},
);
}
}
const cause = new Error('original');
const wrapped = new WrappedError(cause) as any;
expect(wrapped.cause).toBe(cause);
});
it('should always have stack property even without throw', () => {
// in IE, the stack will be undefined without throw
// in ZoneAwareError, we will make stack always be