我有一个自定义错误类:
class NetworkError extends Error {
constructor() {
super('Network Error');
this.name = 'NetworkError';
}
}
我想特别处理它:
import {NetworkError} from '../../common/errors';
someFunc().catch(NetworkError, err => {
// this is missed
}).catch(err => {
// this is hit
});
但它正在跳过我的自定义捕获并击中一般捕获。如果我这样改变它,它可以工作:
someFunc().catch({name: 'NetworkError'}, err => {
// this is hit
}).catch(err => {
// this is missed
});
显然第一种方式是首选。我在这里缺少什么?
答案 0 :(得分:0)
正如@loganfsmyth在问题评论中所建议的那样,这是一个巴别塔限制。这个答案可以解决问题:
Why doesn't instanceof work on instances of Error subclasses under babel-node?