在这里,我在 Node.js 中创建了自定义错误类。我创建了这个 ErrorClass 来发送 API 调用的自定义错误响应。
我想赶上这CustomError
门课Bluebird Catch promises
。
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);
我想将其转换为节点模块,但我不知道如何做到这一点。