参考此,ES6中的自定义错误可以写如下:
class MyError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'MyError';
}
}
由于
super()
调用,因此不需要this.stack = (new Error()).stack;
技巧。
然而,我在Babel 下测试它
class MyError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'MyError';
//this.stack = (new Error()).stack;
//Error.captureStackTrace(this, this.constructor.name);
}
}
var myerror = new MyError("test");
console.log(myerror.stack)
除非调用代码this.stack = (new Error()).stack;
或Error.captureStackTrace(this, this.constructor.name);
,否则没有堆栈信息
但我在Chrome console
下测试了上面没有this.stack = (new Error()).stack;
或Error.captureStackTrace(this, this.constructor.name);
的代码片段。
输出:
MyError: test
at MyError (<anonymous>:3:28)
at <anonymous>:12:19
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
at Object.InjectedScript.evaluate (<anonymous>:664:21)
我应该认为这是Babel
或Chrome问题上的一个缺陷吗?还是错过了理解super()
?
更新
根据V8代码,Error
的stack
captureStackTrace = function captureStackTrace(obj, cons_opt) {
// Define accessors first, as this may fail and throw.
ObjectDefineProperty(obj, 'stack', { get: StackTraceGetter,
set: StackTraceSetter,
configurable: true });
根据我的理解,由于stack
是Error
的一个属性,所以在调用supper()
之后,就没有必要在MyError
类中调用captureStackTrace
。我是不是错过了什么?
据我所知,为了在V8中正确扩展Error
类,您应该调用
Error.captureStackTrace(this, this.constructor)
在它的构造函数中,例如:
class MyError extends Error {
constructor (message) {
super()
Error.captureStackTrace(this, this.constructor)
Object.assign(this, {name: 'MyError', message})
}
}
注意:请记住,并非所有浏览器都支持Error.captureStackTrace
,因此您可能必须将其设为可选。
或者,您可以使用es6-error
npm模块来自动处理所有这些东西:
import ExtendableError from 'es6-error';
class MyError extends ExtendableError {
// everything is taken care of
}