如何通过ES错误类避免堆栈溢出

时间:2018-09-03 15:53:07

标签: 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)

有什么想法会导致溢出以及如何解决?

2 个答案:

答案 0 :(得分:1)

这是harmony-reflect库中的错误,该错误显示在您的堆栈跟踪中。似乎是用损坏的功能代替了现有的Reflect.construct功能。我已将其提交到https://github.com/tvcutsem/harmony-reflect/issues/81

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

答案 1 :(得分:0)

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