简体   繁体   English

在 Babel 下调用 super() 的自定义错误无法输出堆栈?

[英]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 to super() 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.根据我的理解,由于stackError一个属性,所以在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.

相关问题 自定义 babel 插件 - 无法遍历(访问)JSXElement - Custom babel plugin - cannot traverse (visit) JSXElement 找不到模块 'babel-loader' 需要堆栈: - Cannot find module 'babel-loader' Require stack: Gulp和Babel:错误:找不到模块 - Gulp and Babel: Error: Cannot find module 错误:无法解析模块“babel-loader” - Error: Cannot resolve module 'babel-loader' ES6(Babel) - 不能在类定义之外调用扩展类的super.methodName - ES6 (Babel) - cannot call super.methodName of an extended class outside the class definition babel 或 webpack 或我的代码导致此错误“未捕获的类型错误:超级表达式必须是 null 或函数” - either babel or webpack or my code are causing this error "Uncaught TypeError: Super expression must either be null or a function" Reactjs错误:babel-runtime.js:32 Uncaught TypeError:超级表达式必须为null或函数,而不是未定义 - Reactjs error: babel-runtime.js:32 Uncaught TypeError: Super expression must either be null or a function, not undefined Babel-node无法在node_modules下编译js,如何解决? - Babel-node cannot compile js under node_modules, how to resolve it? 多应用程序中的错误:将babel升级到v7后无法解析模块“ babel-loader” - Error in multi app: Cannot resolve module 'babel-loader' after upgrading babel to v7 如何将 babel 和 webpack 实现更新和整合到当前的最佳实践中? (错误:找不到模块“@babel/core”) - How to update and consolidate babel and webpack implementations to current best practices? (Error: Cannot find module '@babel/core')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM