Skip to content

Should the stack getter on a non-Error return undefined or an empty string? #3

Closed

Description

@mgaudet

Looking at the Firefox implementation, it appears that we explicitly handle the prototype chain in our getter implementation:

This means that

let o = {__proto__: new Error}
o.stack

works; as I read the spec text on this proposal it would currently throw however because o lacks an [[ErrorData]] internal slot.

Activity

  1. ljharb commented on Jan 9, 2025

    @ljharb
    Member

    Your analysis is correct. Is there code on the web that relies on this working? It's very uncommon in the language to have accessors that don't brand check.

  2. mgaudet commented on Jan 9, 2025

    @mgaudet
    Author

    No idea. We don't have telemetry here to tell us.

  3. ljharb commented on Jan 9, 2025

    @ljharb
    Member

    What is the appetite for shipping with the check with a pre-agreement to remove it if there's web compat issues?

    Meaning, we could easily have two specs - main, plus a PR that removes the checks - and in theory a flag could guard the two variants that could be flipped post-deploy?

  4. mgaudet commented on Jan 9, 2025

    @mgaudet
    Author

    My appetite is fairly low; I wouldn't be opposed to trying to get some telemetry but not sure when I can slot that in.

  5. nicolo-ribaudo commented on Jan 10, 2025

    @nicolo-ribaudo
    Member

    Note that the current spec text returns undefined, rather than throwing.

    If E does not have an [[ErrorData]] internal slot, return undefined.

  6. ljharb commented on Jan 10, 2025

    @ljharb
    Member

    Indeed, closing makes sense, I believe.

  7. nicolo-ribaudo commented on Jan 10, 2025

    @nicolo-ribaudo
    Member

    Well, it still behaves differently from Firefox and Chrome where o.stack returns a string and not undefined :)

    (I have no opinion on what we should do, I'd go with the current behavior if it's web-compatible)

  8. mgaudet commented on Jan 10, 2025

    @mgaudet
    Author

    I'd leave this open until we had any idea at all what is web compatible.

  9. ljharb commented on Jan 10, 2025

    @ljharb
    Member

    Sounds good

  10. reopened this on Jan 10, 2025
  11. mgaudet commented on Jan 10, 2025

    @mgaudet
    Author

    (It does appear that Chrome also works this way)

  12. ljharb commented on Nov 7, 2025

    @ljharb
    Member

    @mgaudet which way does Chrome work? Returns undefined, or returns a string?

  13. mgaudet commented on Nov 7, 2025

    @mgaudet
    Author

    Returns a string.

  14. ljharb commented on Nov 9, 2025

    @ljharb
    Member

    So, the only remaining question here is, is it web compatible to return undefined vs an empty string?

    Since both are falsy, the only issue would be if something unconditionally called a string method on a .stack from a non-Error that still somehow had the Error.prototype stack getter.

    I suspect this is exceedingly unlikely.

  15. changed the title [-]What about errors on the prototype chain? [/-] [+]Should the stack getter on a non-Error return undefined or an empty string?[/+] on Nov 9, 2025
  16. mhofman commented on Nov 9, 2025

    @mhofman
    Member

    Since both are falsy, the only issue would be if something unconditionally called a string method on a .stack

    It's a little more complicated. The value could simply be serialized and sent. In the case of JSON it'd mean the property would disappear.

    a non-Error that still somehow had the Error.prototype stack getter

    I think this constraint means it's very unlikely, but it would be great to get some telemetry on this however.

  17. ljharb commented on Nov 18, 2025

    @ljharb
    Member

    Now that the setter throws on non-strings (per today's plenary), I'm planning to stick with returning undefined. Please let me know if there's telemetry or evidence compelling a different path.

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