Skip to content

fix(js/ts): make Exception.ToString() return the message instead of "Exception"#4633

Closed
klofberg wants to merge 1 commit into
fable-compiler:mainfrom
reaptor:fix/exception-tostring-message
Closed

fix(js/ts): make Exception.ToString() return the message instead of "Exception"#4633
klofberg wants to merge 1 commit into
fable-compiler:mainfrom
reaptor:fix/exception-tostring-message

Conversation

@klofberg

@klofberg klofberg commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Fix: Exception.ToString() returns "Exception" instead of the message (JS/TS)

Problem

Since v2, calling ToString() on a caught exception prints the literal string "Exception" instead of the message:

try
    failwith "boom"
with ex ->
    Browser.Dom.console.log(ex.ToString())
// v1: "boom"  →  v2: "Exception"

Cause

PR #4197 ("[JS/TS] Replace Error with Exception") changed System.Exception so it compiles to Fable's own Exception class (in Util.ts) rather than JavaScript's native Error. That class — intentionally not derived from Error for performance (#2160) — has no toString() method, so ex.ToString() (routed through Types.toString) falls back to returning the constructor name. Previously, native Error.prototype.toString() returned "<name>: <message>", so the message was visible.

Fix

Add a toString() to the Exception class returning this.message. This matches the Dart (ExceptionBase.toString() => this.message) and Python (native __str__) targets, which were already correct. F# exception declarations (FSharpException) keep their own record-formatting toString(), and the BCL exception subclasses in System.fs inherit the new behavior.

Scoped to the JS/TS runtime library only; no compiler changes needed.

Testing

  • Added a regression test in tests/Js/Main/TypeTests.fs asserting ex.ToString() contains the message (passes on both .NET and Fable).
  • Verified via quicktest (ex.ToString() now prints boom) and the full JS test suite (no regressions).

…eption"

Since fable-compiler#4197 System.Exception compiles to Fable's own Exception class rather
than native Error. That class had no toString(), so ex.ToString() fell back
to the constructor name ("Exception") instead of the message. Add a toString()
returning the message, matching the Dart and Python targets.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@MangelMaxime MangelMaxime changed the title fix(js): make Exception.ToString() return the message instead of "Exception" fix(js/ts): make Exception.ToString() return the message instead of "Exception" Jun 9, 2026
@MangelMaxime

Copy link
Copy Markdown
Member

Replaced by #4635

I could not push to you fork, so I needed to make a new PR.

Thank you for the fix

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants