Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect capturing of stacktrace in custom errors #6216

Closed
rostik404 opened this issue Sep 8, 2017 · 3 comments
Closed

Incorrect capturing of stacktrace in custom errors #6216

rostik404 opened this issue Sep 8, 2017 · 3 comments
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@rostik404
Copy link

After babel transformation, custom error loses its stacktrace because of incorrect value in _this.constructor.

Input Code

Here is a simple example of custom js Error:

Test = class Test extends Error {
    constructor() {
        super();
        Error.captureStackTrace(this, this.constructor)
        console.log('Constructor: ', this.constructor);
    }
}

const e = new Test('some-error')
console.log('Stack: ', e.stack);

I'm expecting to get Test in constructor and full stacktrace in e.stack, and everything works as I expect:

Constructor:  class Test extends Error {
    constructor() {
        super();
        Error.captureStackTrace(this, this.constructor)
        console.log('Constructor: ', this.constructor);
    }
}
Stack:  Error
    at Object.<anonymous> (/Users/rostik404/projects/test-project/test-error.js:42:11)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:974:3

After babel transformation, I'm getting this code:

var Test = function (_Error) {
    _inherits(Test, _Error);

    function Test() {
        _classCallCheck(this, Test);

        var _this = _possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));

        Error.captureStackTrace(_this, _this.constructor);
        console.log('Constructor: ', this.constructor);
        return _this;
    }

    return Test;
}(Error);

var e = new Test('some-error');
console.log('Stack: ', e.stack);

And in this code, I'm getting Error in _this.constructor instead of Test and because of this Error.capturestacktrace cuts the whole stacktrace:

Constructor:  function Error() { [native code] }
Stack:  Error

Babel Configuration (.babelrc, package.json, cli command)

Using babel online

Expected Behavior

Full stacktrace in custom Error

Current Behavior

Cut stacktrace and incorrect value in _this.constructor

Your Environment

software version(s)
Babel v6.26.0
node v8.4.0
npm v5.3.0
Operating System macOS Sierra v10.12.4
@babel-bot
Copy link
Collaborator

Hey @rostik404! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@loganfsmyth
Copy link
Member

loganfsmyth commented Sep 8, 2017

.name is Error because we don't support extending builtin types out of the box: https://babeljs.io/docs/usage/caveats/#classes

That said, even using that builtin-extend plugin, which makes .name Test, the stack trace itself does seem to still contain Error. I'm not sure there's anything we can do there, I have no idea how Chrome decides what to use when building traces.

We likely can't easily fix that.

@hzoo
Copy link
Member

hzoo commented Sep 13, 2017

I guess we'll close then? Can try #4480 (comment)

@hzoo hzoo closed this as completed Sep 13, 2017
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

5 participants