Skip to content

3.20.0 Error cause wrapper breaks some v8/node.js stack trace apis #1061

@dougwilson

Description

@dougwilson

Hello 👋

It seems that with the release of 3.20.0, the new Error cause support is overwriting the global Error object with one from this module. This is in order to support the new options argument to the constructor for cause support. The downside is there are a few "magical" properties on the Error object that provide some stack trace APIs like Error.stackTraceLimit and Error.prepareStackTrace. There are modules like https://www.npmjs.com/package/stack-trace and it's 737 dependents that end up with unexpected behavior when the case is being polyfilled.

I started to take a look around, and a quick and dirt change that worked was adding something like the following in https://github.com/zloirock/core-js/blob/de16b4013b8de5b713437027cee6ec4d70831ccc/packages/core-js/internals/wrap-error-constructor-with-cause.js :

    if (ERROR_NAME === 'Error') {
      Object.defineProperties(WrappedError, {
        prepareStackTrace: {
          get: function () { return OriginalError.prepareStackTrace; },
          set: function (v) { OriginalError.prepareStackTrace = v; }
        },
        stackTraceLimit: {
          get: function () { return OriginalError.stackTraceLimit; },
          set: function (v) { OriginalError.stackTraceLimit = v; }
        }
      });
    }

That basically proxies the two properties back to the original Error object which v8 then is able to read when it performs the stack trace generation logic.

I'm not sure that is the full fix (like Object.defineProperties should probably use the impl that is in this module, it needs tests probably, etc.), but I just wanted to open this issue at least for now as I recently sustained a bad injury that makes typing quite painful at the moment. Perhaps someone will be gracious enough to develop a fix while I rest and heal.

Thanks for reading.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions