fix(js/ts): make Exception.ToString() return the message instead of "Exception"#4633
Closed
klofberg wants to merge 1 commit into
Closed
fix(js/ts): make Exception.ToString() return the message instead of "Exception"#4633klofberg wants to merge 1 commit into
klofberg wants to merge 1 commit into
Conversation
…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>
Member
|
Replaced by #4635 I could not push to you fork, so I needed to make a new PR. Thank you for the fix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:Cause
PR #4197 ("[JS/TS] Replace Error with Exception") changed
System.Exceptionso it compiles to Fable's ownExceptionclass (inUtil.ts) rather than JavaScript's nativeError. That class — intentionally not derived fromErrorfor performance (#2160) — has notoString()method, soex.ToString()(routed throughTypes.toString) falls back to returning the constructor name. Previously, nativeError.prototype.toString()returned"<name>: <message>", so the message was visible.Fix
Add a
toString()to theExceptionclass returningthis.message. This matches the Dart (ExceptionBase.toString() => this.message) and Python (native__str__) targets, which were already correct. F#exceptiondeclarations (FSharpException) keep their own record-formattingtoString(), and the BCL exception subclasses inSystem.fsinherit the new behavior.Scoped to the JS/TS runtime library only; no compiler changes needed.
Testing
tests/Js/Main/TypeTests.fsassertingex.ToString()contains the message (passes on both .NET and Fable).ex.ToString()now printsboom) and the full JS test suite (no regressions).