Closed Bug 1710694 Opened 3 years ago Closed 3 years ago

Wrong error message when a custom error is thrown or logged to the console

Categories

(DevTools :: Console, defect, P3)

Firefox 88
defect

Tracking

(firefox90 verified)

VERIFIED FIXED
90 Branch
Tracking Status
firefox90 --- verified

People

(Reporter: marvin, Assigned: nchevobbe)

Details

Attachments

(2 files)

Attached file index.html

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0

Steps to reproduce:

  1. Download attached html file
  2. Open it in a browser
  3. 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.

Summary: Message getter not called on custom Error → Wrong error message when a custom error is thrown
Summary: Wrong error message when a custom error is thrown → Wrong error message when a custom error is thrown or logged to the console

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.

Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core

According to Console source code, the right component must be JS Engine.

Component: DOM: Core & HTML → JavaScript Engine
Component: JavaScript Engine → Console
Product: Core → DevTools
Severity: -- → S3
Flags: needinfo?(nchevobbe)
Priority: -- → P3
Status: UNCONFIRMED → NEW
Ever confirmed: true

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: nobody → nchevobbe
Status: NEW → ASSIGNED
Flags: needinfo?(nchevobbe)

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.
Status: ASSIGNED → RESOLVED
Closed: 3 years ago
Resolution: --- → FIXED
Target Milestone: --- → 90 Branch
Flags: qe-verify+

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.

Status: RESOLVED → VERIFIED
Flags: qe-verify+

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?

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: