javascript - es错误类如何避免栈溢出

标签 javascript babeljs stack-overflow

我创建了一个自定义错误类,在我更改 babel 配置之前它工作正常,现在抛出堆栈溢出错误。

这是错误类(为了重现而精简):

export default class UserError extends Error {
    constructor(code, message, innerError, hint) {
        super();
    }
}

这是我的 .babelrc 文件(我使用的是 babel 7):

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": [
        "@babel/plugin-proposal-export-default-from",
        "@babel/plugin-proposal-logical-assignment-operators",
        ["@babel/plugin-proposal-optional-chaining", { "loose": false }],
        ["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }],
        ["@babel/plugin-proposal-nullish-coalescing-operator", { "loose": false }],
        "@babel/plugin-proposal-do-expressions",

        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        "@babel/plugin-proposal-function-sent",
        "@babel/plugin-proposal-export-namespace-from",
        "@babel/plugin-proposal-numeric-separator",
        "@babel/plugin-proposal-throw-expressions",

        "@babel/plugin-syntax-dynamic-import",
        "@babel/plugin-syntax-import-meta",
        ["@babel/plugin-proposal-class-properties", { "loose": false }],
        "@babel/plugin-proposal-json-strings"
    ]
}

这是我得到的错误:

  RangeError: Maximum call stack size exceeded
      at UserError.Wrapper (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:18:461)
      at new UserError (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:2:48)
      at construct (/Volumes/Development/Development/Xolvio/xspecs/node_modules/harmony-reflect/reflect.js:2086:12)
      at UserError.Wrapper (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:18:473)
      at new UserError (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:2:48)
      at construct (/Volumes/Development/Development/Xolvio/xspecs/node_modules/harmony-reflect/reflect.js:2086:12)
      at UserError.Wrapper (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:18:473)
      at new UserError (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:2:48)
      at construct (/Volumes/Development/Development/Xolvio/xspecs/node_modules/harmony-reflect/reflect.js:2086:12)
      at UserError.Wrapper (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:18:473)
      at new UserError (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:2:48)
      at construct (/Volumes/Development/Development/Xolvio/xspecs/node_modules/harmony-reflect/reflect.js:2086:12)
      at UserError.Wrapper (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:18:473)
      at new UserError (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:2:48)
      at construct (/Volumes/Development/Development/Xolvio/xspecs/node_modules/harmony-reflect/reflect.js:2086:12)
      at UserError.Wrapper (/Volumes/Development/Development/Xolvio/xspecs/modules/utilities/error.js:18:473)

知道是什么原因导致溢出以及如何解决它吗?

最佳答案

这是 harmony-reflect 库中的错误,它显示在您的堆栈跟踪中。它似乎用损坏的函数替换了现有的 Reflect.construct 函数。我已将其归档在 https://github.com/tvcutsem/harmony-reflect/issues/81 中.

您需要找出加载该库的内容并查看是否可以将其删除。不幸的是,它似乎正在创建一个不符合规范并破坏 Babel 输出代码的 Reflect.construct 函数。

关于javascript - es错误类如何避免栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52152988/

相关文章:

java - stackoverflow异常的原因是什么?

java - AWT-EventQueue-0“StackOverflowError

javascript - 如何将 xmlhttprequest 转换为字符串 javascript

javascript - 在javascript中用正则表达式替换

node.js - gcloud App Engine Flexible Strangeness 与 Docker 和 Babel

java - 从 JavaScript 调用 Java 时出现不可恢复的 stackoverflow 错误

javascript - 使用javascript重命名输入中的文本(每点更改空间)

javascript - 使用 for 循环通过 array.length 向后循环 javascript 数组

npm - Babel CLI 非常慢

javascript - ES6 全局变量在类中不可用