如何避免堆栈溢出并出现 es 错误 class

How to avoid stack overflow with es error class

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

这是错误 class(为了重现而删除):

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)

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

截断的代码非常有限,我能看到的是如果你想让它提供你的错误,你必须在 super() 中传递参数 class。可能这会导致错误,被组件捕获,从而导致无限循环。

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

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