【发布时间】:2025-12-03 23:45:02
【问题描述】:
在大型公共库 (pg-promise) 中为 ES6 重构自定义错误后,我收到一些奇怪的报告,即自定义错误在某些特殊情况下可能无法正确实例化,我无法重现,经过多次尝试。
是否有实施自定义错误经验的人,请告诉我所做的重构是否 1:1 正确,或者我是否遗漏了什么。
原始 ES5 代码
function CustomError(errCode) {
var temp = Error.apply(this, arguments);
temp.name = this.name = 'CustomError';
this.stack = temp.stack;
this.code = errCode;
}
CustomError.prototype = Object.create(Error.prototype, {
constructor: {
value: CustomError,
writable: true,
configurable: true
}
});
CustomError.prototype.toString = function () {
console.log('errCode:', this.code);
};
CustomError.prototype.inspect = function () {
return this.toString();
};
重构的 ES6 代码:
class CustomError extends Error {
constructor(errCode) {
super();
Error.captureStackTrace(this, CustomError);
this.code = errCode;
}
}
CustomError.prototype.toString = function () {
console.log('errCode:', this.code);
};
CustomError.prototype.inspect = function () {
return this.toString();
};
这两个示例都要求在任何 Node.js 4.x 及更高版本下都可以正常工作,实例化为:
const error = new CustomError(123);
console.log(error);
根据错误报告,有时这样的类型被认为是在没有正确的 this 上下文的情况下创建的,或者更准确地说,错误表示:can't use 'code' of undefined that's inside toString。
更新
看完这篇文章:Custom JavaScript Errors in ES6,及其例子:
class GoodError extends Error {
constructor(...args) {
super(...args)
Error.captureStackTrace(this, GoodError)
}
}
似乎传递这样的参数是我所缺少的。但是,考虑到以下情况,我怎样才能正确更新我的 CustomError 类以执行相同的操作:
- 语法
...args在 Node.js 4.x 中不起作用,所以我不能使用它 - 我需要将它与现有的custruction参数结合起来,例如
errCode
【问题讨论】:
-
需要注意的一点是
super()应该是super(arguments)。 -
@destoryer 不应该是
super(...arguments)吗? -
@PeterMader 是的,谢谢。
-
我想不出你能做些什么来阻止
this上下文被正确传递。 -
为什么要使用类语法和原型语法?为什么不使用一个?