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

Babel generated Error subclass produces instance of Error rather than instance of subclass #4485

Closed
Gozala opened this issue Sep 8, 2016 · 10 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Gozala
Copy link

Gozala commented Sep 8, 2016

Input Code

class ReadError extends Error {}

new ReadError('Boom') instanceof ReadError // => false

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

https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015&experimental=true&loose=false&spec=false&playground=false&code=class%20ReadError%20extends%20Error%20%7B%7D%0A%0Aalert(new%20ReadError('Boom')%20instanceof%20ReadError)%20%2F%2F%20%3D%3E%20false

Expected Behavior

should alert true

Current Behavior

alerts false

@loganfsmyth
Copy link
Member

https://stackoverflow.com/questions/33870684/why-doesnt-instanceof-work-on-instances-of-error-subclasses-under-babel-node/33877501#33877501 should cover it.

We mention it here in our Caveats: https://babeljs.io/docs/usage/caveats/#classes but if you have recommendations of a better place let us know.

@forivall
Copy link
Contributor

forivall commented Sep 9, 2016

It's also interesting to note that somehow, it works in node 0.10

https://gist.github.com/forivall/88d8056d87bcc566a5d02ef837d772c6

I'm curious as to how that works, and this could be noted on the caveats page as well.

As far as a better place, it could go in something like "known issues"?

@Gozala
Copy link
Author

Gozala commented Sep 9, 2016

I would prefer if babel would just error and not compile on attempt to subclass built-ins that can't be. That being said I don't know if work required is worth the benefits.

@Gozala Gozala closed this as completed Sep 9, 2016
@hzoo
Copy link
Member

hzoo commented Sep 9, 2016

We can at least make an option for that (fail when subclassing built-ins)

@xpl
Copy link

xpl commented Jul 15, 2017

Try this as a workaround:

class DDoSProtectionError extends Error {
    constructor () {
        super ()
        // a workaround to make `instanceof DDoSProtectionError` work in ES5
        this.constructor = DDoSProtectionError 
        this.__proto__   = DDoSProtectionError.prototype
    }
}

@Leonid-Yeromin
Copy link

Almost year, the problem still exists. WTF?

@hzoo
Copy link
Member

hzoo commented Aug 2, 2017

? You can use #4480 (comment)

@rico345100
Copy link

rico345100 commented Apr 2, 2018

@xpl Worked with 8.x, but not working with 6.9.5
@hzoo Doesn't worked with Node.js 8.9.0

@rico345100
Copy link

@nicolo-ribaudo That's awesome. Note that I just fixed this issue.

  1. Adding babel-plugin-transform-builtin-extend
  2. Do what @xpl suggests

Now it works.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 2, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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

8 participants