Catch Error Type in Bluebird Not Working

徘徊边缘 提交于 2019-12-29 02:09:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!