[英]Stack cannot be output for custom error with super() called under Babel?
Refer this , the Custom Errors in ES6 can be written as following:参考这个,ES6 中的自定义错误可以写成如下:
class MyError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'MyError';
}
}
There is no need for
this.stack = (new Error()).stack;
不需要
this.stack = (new Error()).stack;
trick thanks tosuper()
call.技巧感谢
super()
调用。
However, I test it under Babel不过我是在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)
There is NO stack information unless the code this.stack = (new Error()).stack;
没有堆栈信息,除非代码
this.stack = (new Error()).stack;
or Error.captureStackTrace(this, this.constructor.name);
或
Error.captureStackTrace(this, this.constructor.name);
is invoked.被调用。
But I test the above code snippet without this.stack = (new Error()).stack;
但是我在没有
this.stack = (new Error()).stack;
情况下测试了上面的代码片段this.stack = (new Error()).stack;
or Error.captureStackTrace(this, this.constructor.name);
或
Error.captureStackTrace(this, this.constructor.name);
under Chrome console
.在
Chrome console
。
Output:输出:
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)
Should I consider this is one defect on Babel
or Chrome issue?我应该认为这是
Babel
或 Chrome 问题的一个缺陷吗? Or miss understanding the super()
?还是想念理解
super()
?
Update更新
According to V8 codes, the stack
of Error
根据 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 });
Base on my understanding, since the stack
is one property of Error
, after supper()
is called, then it is not necessary to invoke captureStackTrace
in MyError
class.根据我的理解,由于
stack
是Error
一个属性,所以在stack
supper()
之后,就没有必要在MyError
类中调用captureStackTrace
了。 Am I missing something?我错过了什么吗?
As far as I know, in order to properly extend Error
class in V8 you should call据我所知,为了在 V8 中正确扩展
Error
类,您应该调用
Error.captureStackTrace(this, this.constructor)
in it's constructor, eg:在它的构造函数中,例如:
class MyError extends Error {
constructor (message) {
super()
Error.captureStackTrace(this, this.constructor)
Object.assign(this, {name: 'MyError', message})
}
}
NB: Bear in mind that not all browsers support Error.captureStackTrace
, so you may have to make it optional.注意:请记住,并非所有浏览器都支持
Error.captureStackTrace
,因此您可能必须将其设为可选。
Alternatively, you could use es6-error
npm module to deal with all this stuff automatically:或者,您可以使用
es6-error
npm 模块自动处理所有这些内容:
import ExtendableError from 'es6-error';
class MyError extends ExtendableError {
// everything is taken care of
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.