-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(node/browser): Always use constructor to get error type #8161
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
fix(node/browser): Always use constructor to get error type #8161
Conversation
size-limit report 📦
|
389c5be
to
7a65ac5
Compare
7a65ac5
to
a1933a7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the tradeoff here is that if we go through with that change, manually set error.name
properties wouldn't be respected anymore?
class CustomError extends Error {
constructor(msg) {
super(msg);
this.name = 'MyCustomError';
}
}
or fwiw
const err = new Error();
err.name = 'whatever'
I'm not sure yet if this is an improvement tbh
I'm a little confused by this example: class CustomError extends Error {
constructor(msg) {
super(msg);
this.name = 'MyCustomError';
}
} If you name your class That said, I just played around a bit, and I think there's another way to solve this problem. Using the same example as above: (The first class and If we do it that way, we can keep pulling the error type from |
Sorry, I should have taken the
Clever! All right, lemme give this a go in another branch. P.S. I stuck this in a replit so I wouldn't have to retype it. Sorry, should have shared that from the get-go: https://replit.com/@lobsterkatie/RewardingSlowVideogame#index.js |
Hey, quick heads up here: Cramer discovered that when doing Example: https://cramer.sentry.io/issues/4203128674/events/5f03c3b3b8604d7a8ef611ef5c6a701a/ Given the above, should we still go forward with the PR? |
Huh. Interesting. I suppose that's perhaps because the minifier happily mangles the names of classes but won't minify certain reserved tokens, inlcuding I'm happy to close this for now. If I wake up in the middle of the night with a brilliant revelation, I can always re-open it. |
That's the way :D |
For built-in subclasses of
Error
, JavaScript adjusts thename
property to match the name of the class, but it does not do the same for user-definedError
subclasses. This therefore switches to always usingconstructor.name
, as it works for both built-in and custom subclasses ofError
.Note: Our tracekit tests rely on fake error objects, rather than real instances of
Error
or a subclass, in order to be able to control the stacktrace value. In order to get those tests to pass, I added a helper to convert the mock errors into ones which also have the correctconstructor.name
property. I also had to hack react error boundary error creation just a tiny bit. LMK if you think there's a better way to do that.