我在Node.js中创建了自定义错误类。我创建这个ErrorClass是为了发送API调用的自定义错误响应。
我想在Bluebird Catch promises
中捕获这个CustomError
类。
Object.defineProperty(Error.prototype, 'message', {
configurable: true,
enumerable: true
});
Object.defineProperty(Error.prototype, 'stack', {
configurable: true,
enumerable: true
});
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
var alt = {};
Object.getOwnPropertyNames(this).forEach(function (key) {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true
});
Object.defineProperty(Error.prototype, 'errCode', {
configurable: true,
enumerable: true
});
function CustomError(errcode, err, message) {
Error.captureStackTrace(this, this.constructor);
this.name = 'CustomError';
this.message = message;
this.errcode = errcode;
this.err = err;
}
CustomError.prototype = Object.create(Error.prototype);
我想把这个转换成 Node.js 模块,但是我不知道如何做。
message
、stack
和toJSON
添加到Error.prototype
中?为什么不直接在CustomError
中设置它们呢? - thefourtheyemessage
/stack
,它们已经从Error.prototype
继承了。 - James