Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

promises rejected with a specific error are not of the original type when they are caught (v0.8.10 reproduction included) #797

Closed
dbpieter opened this issue Jun 1, 2017 · 2 comments

Comments

@dbpieter
Copy link

dbpieter commented Jun 1, 2017

When a promise is rejected with a specific error it is not of the same type once it is caught.
REPRO: https://plnkr.co/edit/7fASDCWYk0kRqax5oUWz?p=preview

class MyError extends Error {}

Promise.reject(new MyError()).catch((err) => {
  if(err instanceof MyError) {
    console.log('workz')
  } else {
    console.log('wtf!') // zone v0.8.10 :(
  }
})

This doesn't work as expected when zone v0.8.10 is loaded.
Loading 'zone-js/dist/zone-error.js' fixes the issue. Is it meant to be lke this.

Seems strange this additional script needs to be loaded in order for Zone to not shadow error types.
Zone should be completely transparant right ?

@dbpieter dbpieter changed the title promises rejected with a specific error are not of the original type when they are caught i (v0.8.10 reproduction included) promises rejected with a specific error are not of the original type when they are caught (v0.8.10 reproduction included) Jun 1, 2017
@JiaLiPassion
Copy link
Collaborator

@dbpieter , I don't think this behavior related to zone.js, you can just try use typescript 2.x to write this sample code.

class MyError extends Error {}

const error = new MyError();

console.log('instanceof', error instanceof MyError);

the result will be false.

The typescript compiled code will look like.

var MyError = (function (_super) {
    __extends(MyError, _super);
    function MyError() {
        return _super.apply(this, arguments) || this;
    }
    return MyError;
}(Error));

the new MyError() will always return an Error instance not a MyError.

So this is more like a typescript behavior.

When you load zone-js/dist/zone-error.js, new MyError() will return MyError instance.

@dbpieter
Copy link
Author

dbpieter commented Jun 2, 2017

@JiaLiPassion. Thx for your explanation. The behaviour changed with zone v0.8.10 so I thought this was zone related.

It seems it is better not to extend from Error at all.

@dbpieter dbpieter closed this as completed Jun 2, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants