-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Extending builtins like Error and Array doesn't work in 6.x #3083
Comments
Extending builtin types like
Well, yes... "unsupported" but the reality is that you only go find that out when things don't work. So in Babel 5, nobody bothered since the basic use-case did what it was supposed to. Despite that, a regression is a regression, the behaviour has changed between the versions, you would expect a class to show as it's own instanceof despite the parent,
Unfortunately, what the Error class constructor essentially does is this:
It worked in Babel 5 because Babel did not support a class constructor I do have an alternate approach for you though. Do this:
and use that in your example as:
I'd still not recommend extending builtins like this, but it will make the code behave like it did in Babel 5.x
Old news by now, but posting in here so people can find it. All of the logic I mention above is wrapped up and available in https://www.npmjs.com/package/babel-plugin-transform-builtin-extend as an easy-to-use package. |
`(new HTTPError()) instanceof HTTPError` was false because babel-plugin-transform-es2015-classes doesn't support extending of built-in classes. - https://babeljs.io/docs/plugins/transform-es2015-classes/ - babel/babel#3083 - http://stackoverflow.com/a/33877501 To fix this issue, This commit removes babel-preset-es2015 except for transform-es2015-modules-commonjs. The current LTS version of nodejs is v6.9.1 and it supports most of es2015 features according to http://node.green/.
* several files renames (to make the sources more understandable) * add `builtin` package for babel (see babel/babel/issues/3083 ) * switch testing to the DIST version of package (not sources) * probably fix the problems in browser testing (`instance of` now works), see #49
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
}
} |
|
@xpl you saved my day! It's the only working solution. |
* Add `babel-plugin-transform-builtin-extend` * Subclassing builtin types needs special treatment by babel * Without this plugin, in a node environment, a subclass of Error does not actually behave like a true subclass, which manifests itself via a missing stacktrace when a StitchError is thrown More details [here](babel/babel#3083)
* Add `babel-plugin-transform-builtin-extend` * Subclassing builtin types needs special treatment by babel * Without this plugin, in a node environment, a subclass of Error does not actually behave like a true subclass, which manifests itself via a missing stacktrace when a StitchError is thrown More details [here](babel/babel#3083)
* Add `babel-plugin-transform-builtin-extend` * Subclassing builtin types needs special treatment by babel * Without this plugin, in a node environment, a subclass of Error does not actually behave like a true subclass, which manifests itself via a missing stacktrace when a StitchError is thrown More details [here](babel/babel#3083)
* Add `babel-plugin-transform-builtin-extend` * Subclassing builtin types needs special treatment by babel * Without this plugin, in a node environment, a subclass of Error does not actually behave like a true subclass, which manifests itself via a missing stacktrace when a StitchError is thrown More details [here](babel/babel#3083)
* Add `babel-plugin-transform-builtin-extend` * Subclassing builtin types needs special treatment by babel * Without this plugin, in a node environment, a subclass of Error does not actually behave like a true subclass, which manifests itself via a missing stacktrace when a StitchError is thrown More details [here](babel/babel#3083)
We were relying on being able to override toString in DecryptionError, which (a) doesn't work thanks to babel/babel#3083, and (b) was a bit naughty anyway. Instead, just add a detailedString property and use that.
Code
Babel ^5.8.21
package.json:
.babelrc
output:
^6.1.4
package.json:
.babelrc
output:
The text was updated successfully, but these errors were encountered: