Catching custom error not working in Bluebird

前端 未结 1 1530
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 13:59

I am trying to throw and then catch a custom error in a Bluebird promise chain, but I can\'t get it to catch the custom error. For example:

function login(re         


        
1条回答
  •  一整个雨季
    2021-01-15 14:23

    Bluebird distinguishes error constructors from predicate functions in a catch by their inheritance:

    For a parameter to be considered a type of error that you want to filter, you need the constructor to have its .prototype property be instanceof Error.

    Such a constructor can be minimally created like so:

    function MyCustomError() {}
    MyCustomError.prototype = Object.create(Error.prototype);
    

    You will need to do the same for your LoginError.

    Or if you're using ES6, then class LoginError extends Error {}.

    0 讨论(0)
提交回复
热议问题