问题
I have a custom error class:
class NetworkError extends Error {
constructor() {
super('Network Error');
this.name = 'NetworkError';
}
}
And I want to handle it specifically:
import {NetworkError} from '../../common/errors';
someFunc().catch(NetworkError, err => {
// this is missed
}).catch(err => {
// this is hit
});
But it's skipping my custom catch and hitting the general catch. If I change it like so, it works:
someFunc().catch({name: 'NetworkError'}, err => {
// this is hit
}).catch(err => {
// this is missed
});
Obviously the first way is preferred. What am I missing here?
回答1:
As @loganfsmyth suggested in the question comments, it's a Babel limitation. This answer does the trick:
Why doesn't instanceof work on instances of Error subclasses under babel-node?
来源:https://stackoverflow.com/questions/38927394/catch-error-type-in-bluebird-not-working