6

Bluebird.js のカスタム エラー ハンドラを使用しようとしています。

以下の例では、MyCustomError ハンドラーではなく、catch-all ハンドラーが呼び出されますが、拒否をthen関数に移動した (そして firstPromise を解決した...) と、MyCustomError ハンドラーが呼び出されます。何故ですか?何か間違っていますか?ありがとう。

var Promise = require('bluebird'),
debug = require('debug')('main');

firstPromise()
    .then(function (value) {
      debug(value);
    })
    .catch(MyCustomError, function (err) {
      debug('from MyCustomError catch: ' + err.message);
    })
    .catch(function (err) {
      debug('From catch all: ' + err.message);
    });

/*
 * Promise returning function.
 * */
function firstPromise() {
  return new Promise(function (resolve, reject) {
    reject(new MyCustomError('error from firstPromise'));
  });
}
/*
 *  Custom Error
 * */
function MyCustomError(message) {
  this.message = message;
  this.name = "MyCustomError";
  Error.captureStackTrace(this, MyCustomError);
}
MyCustomError.prototype = Object.create(Error.prototype);
MyCustomError.prototype.constructor = MyCustomError;
4

1 に答える 1

4

何よりも前にエラー クラスを宣言すると、それが機能します (プロトタイプの割り当ては巻き上げられません)。

于 2014-07-15T11:42:18.853 に答える