镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Attempting to console.log() DOMException.prototype throws error #60107

Description

@samualtnorman

Version

v20.19.5, v22.19.0, v24.8.0

Platform

Linux samual-nixos 6.12.48 #1-NixOS SMP PREEMPT_DYNAMIC Fri Sep 19 14:35:52 UTC 2025 x86_64 GNU/Linux

Subsystem

Console

What steps will reproduce the bug?

Evaluate console.log(DOMException.prototype) in any way.

How often does it reproduce? Is there a required condition?

Every time.

What is the expected behavior? Why is that the expected behavior?

Simply print the object to the terminal. That's what browsers do.

What do you see instead?

Welcome to Node.js v24.8.0.
Type ".help" for more information.
> console.log(DOMException.prototype)
Uncaught TypeError: Value of "this" must be of DOMException
    at throwInvalidThisError (node:internal/per_context/domexception:29:15)
    at get name (node:internal/per_context/domexception:133:7)
    at formatError (node:internal/util/inspect:1633:20)
    at formatRaw (node:internal/util/inspect:1067:14)
    at formatValue (node:internal/util/inspect:922:10)
    at inspect (node:internal/util/inspect:409:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2574:40)
    at formatWithOptions (node:internal/util/inspect:2436:10)
    at console.value (node:internal/console/constructor:336:14) {
  code: 'ERR_INVALID_THIS'
}

Additional information

No response

Activity

  1. ljharb commented on Oct 2, 2025

    @ljharb
    SponsorMember

    In a browser, that logs undefined, and it's a data property.

    In node, Object.getOwnPropertyDescriptor(DOMException, 'prototype') also fails, and that should not even be possible.

  2. samualtnorman commented on Oct 2, 2025

    @samualtnorman
    Author

    In a browser, that logs undefined, and it's a data property.

    In node, Object.getOwnPropertyDescriptor(DOMException, 'prototype') also fails, and that should not even be possible.

    how are you getting it to log undefined?
    here's firefox:
    Image
    here's chrome:
    Image

    In Node.js, I assume you're trying to run it in the REPL and the REPL is trying to print the object to the terminal using the same thing console.log() uses (I assume it's using util.inspect() or whatever util.inspect() is an abstraction for).

    Try > void Object.getOwnPropertyDescriptor(DOMException, 'prototype').

  3. ljharb commented on Oct 2, 2025

    @ljharb
    SponsorMember

    You're right, it's the inspection that's causing the problem, and it's failing on the first key name.

    I now see that if i do DOMException.prototype.name or DOMException.prototype directly in the repl, it fails, and in Safari, it just logs undefined (i guess safari's console log implementation logs undefined when it throws). Chrome does something weird and different.

    What does the HTML spec say about this?

  4. Fayti1703 commented on Oct 2, 2025

    @Fayti1703

    What does the HTML spec say about this?

    WebIDL says that invoking a getter on an object that doesn't implement the appropriate interface throws a TypeError. (The case of accessing it on the prototype object is directly called out elsewhere -- note that DOMException::name is not annotated with LegacyLenientThis). So that part seems correct.

    The Console standard is... frustratingly silent on error handling: it says nothing about how to handle errors during printing or formatting. (Which can occur if you try to %s format an object like { toString() { throw new Error("nope") } } -- from a quick test: Firefox seems to silently discard the entire console.log() call in this case, Chrome propagates the exception).

  5. ljharb commented on Oct 2, 2025

    @ljharb
    SponsorMember

    In that case, it seems like node's well within its rights to do whatever it wants here, so while node may choose to do something different, it's not a bug as-is?

  6. samualtnorman commented on Oct 2, 2025

    @samualtnorman
    Author

    Node.js console.log() implementation could throw for every every object passed in if it wanted, that doesn't make it good or useful behaviour.
    It's not a bug in the context of the spec but I don't think the spec is a good context for deciding what's a bug in this case. I think it's a bug in the context of Node.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions