Search code examples
javascriptbabeljsstack-overflow

How to avoid stack overflow with es error class


I've created a custom error class which was working fine until I changed my babel configuration and is now throwing a stack overflow error.

Here's the error class (stripped down for reproduction):

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

Here's my .babelrc file (I'm using 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"
    ]
}

Here's the error I get:

  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)

Any idea what is causing the overflow & how to fix it?


Solution

  • This is a bug in the harmony-reflect library that is showing up in your stack traces there. It appears to replace the existing Reflect.construct function with a broken one. I've filed it in https://github.com/tvcutsem/harmony-reflect/issues/81.

    You'll need to figure out what is loading that library and see if you can remove it. Unfortunately, it appears to be creating a Reflect.construct function that does not conform to the spec and breaks Babel's output code.