Wrong error message when a custom error is thrown or logged to the console
Categories
(DevTools :: Console, defect, P3)
Tracking
(firefox90 verified)
Tracking | Status | |
---|---|---|
firefox90 | --- | verified |
People
(Reporter: marvin, Assigned: nchevobbe)
Details
Attachments
(2 files)
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0
Steps to reproduce:
- Download attached html file
- Open it in a browser
- Check the error message in the console
Actual results:
The error is printed in the console like this:
Uncaught Error: undefined
MyError http://127.0.0.1:8081/:12
<anonymous> http://127.0.0.1:8081/:19
<anonymous> http://127.0.0.1:8081/:20
It seems Firefox picks the original prototype Object to print the error instead of the current class. It does detect "MyError" as can be observed in the stack trace, but the very first line contains just "Error" instead of "MyError".
Expected results:
The message should be present instead of being undefined
Some more findings:
When I rewrite the getter to define the property on this
the message is printed correctly
class MyError extends Error {
get message() {
Object.defineProperty(this, "message", {
value: "my-message",
});
return "my-message";
}
}
Output:
Uncaught Error: my-message
MyError http://127.0.0.1:8081/:12
<anonymous> http://127.0.0.1:8081/:22
<anonymous> http://127.0.0.1:8081/:23
But when I print the error with console.log
or console.error
instead of throwing it, it prints undefined
as the message again. It seems like there are two different code paths in Firefox for printing errors.
Comment 2•3 years ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::DOM: Core & HTML' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.
Comment 3•3 years ago
|
||
According to Console
source code, the right component must be JS Engine.
Updated•3 years ago
|
Updated•3 years ago
|
Updated•3 years ago
|
Assignee | ||
Comment 4•3 years ago
|
||
There are 2 bugs here, the first one is about DevTools not being able to retrieve MyError
. Note that if you ad a name
property to your custom class, this will be picked up:
class MyError extends Error {
name = "MyError"
get message() {
return "my-message";
}
}
The second one is the undefined message, and is caused by https://searchfox.org/mozilla-central/rev/2f1a015b004b79f1145c81cdf86b15481a5630e2/devtools/server/actors/object/previewers.js#603. getProperty
does not retrieve the property if it's a getter (to prevent side effects), but here, I'd say we always want to show the message, and should pass true
as a third parameter.
I'm going to fix this
Assignee | ||
Comment 5•3 years ago
|
||
In the case a custom error object was defining the name
and/or message
property
in getters, we weren't displaying those information in the console.
That's because when retrieving those information, we were avoiding calling getters,
as they're deemed unsafe.
In those specific case, even if unsafe, we always want to show it to the user
so they're not missing any information when debugging.
A test case is added to ensure this works as expected and we don't regress.
Pushed by nchevobbe@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/92b0b44e2c25 [devtools] Fix custom error with message getter. r=ladybenko.
Comment 7•3 years ago
|
||
bugherder |
Updated•3 years ago
|
Comment 8•3 years ago
|
||
I've reproduced this bug on macOS 10.5, using the steps from comment 0, on an affected Nightly build (2021-05-11).
The bug is verified as fixed on Beta 90.0b4, across platforms: Win 10 x64, macOS 10.15 and Ubuntu 18.04 x64.
Comment 9•3 years ago
|
||
I think this makes sense if the getter can be eagerly evaluated like in the example. But otherwise, maybe there should be a "Invoke getter" button like when inspecting objects?
Description
•