fix: convert format parse errors from Internal Error to user-facing messages#1076
Draft
He-Pin wants to merge 1 commit into
Draft
fix: convert format parse errors from Internal Error to user-facing messages#1076He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
…essages
Motivation:
Format string parsing errors (e.g. `std.format("%z", [1])`, `std.format("hello %", [])`)
were thrown as `java.lang.Exception` in `Format.scala`, which propagated up to the
`Interpreter` where they were caught as generic exceptions and wrapped as "Internal Error"
with a full Java stack trace. This produced confusing output compared to go-jsonnet and
jrsonnet, which emit clean one-line error messages.
Modification:
- Wrap `parseFormatCached` and `lowerParsedFormat` calls in `Format.format()` with
try/catch that converts `java.lang.Exception` to `Error.fail(msg, pos)`, producing
clean sjsonnet error messages with position info.
- `Error` exceptions (already-sjsonnet errors) are re-thrown unchanged.
Result:
Format errors now produce clean messages like:
`sjsonnet.Error: [std.format] Unrecognized conversion type: z`
instead of:
`sjsonnet.Error: [std.format] Internal Error`
`Caused by: java.lang.Exception: Unrecognized conversion type: z`
` at sjsonnet.Format$... (full Java stack trace)`
Cross-implementation comparison:
| Expression | sjsonnet (before) | sjsonnet (after) | go-jsonnet | jrsonnet |
|---|---|---|---|---|
| `std.format("%z", [1])` | Internal Error + Java trace | Unrecognized conversion type: z | Unrecognised conversion type: z | unrecognized conversion type: z |
| `std.format("hello %", [])` | Internal Error + Java trace | Truncated format code at end of string | Truncated format code. | truncated format code |
| `std.format("%(key", [1])` | Internal Error + Java trace | Unterminated ( in format spec | Truncated format code. | truncated format code |
| `std.format("%2$s", arr)` | Internal Error + Java trace | Unrecognized conversion type: $ | Unrecognised conversion type: $ | unrecognized conversion type: $ |
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.
Summary
java.lang.ExceptioninFormat.scala, which got caught by the generic exception handler inInterpreterand wrapped as"Internal Error"with a full Java stack traceparseFormatCachedandlowerParsedFormatcalls inFormat.format()with try/catch that convertsjava.lang.ExceptiontoError.fail(msg, pos), producing clean user-facing error messagesCross-implementation comparison
std.format("%z", [1])Internal Error+ Java stack traceUnrecognized conversion type: zUnrecognised conversion type: zunrecognized conversion type: zstd.format("hello %", [])Internal Error+ Java stack traceTruncated format code at end of stringTruncated format code.truncated format codestd.format("%(key", [1])Internal Error+ Java stack traceUnterminated ( in format specTruncated format code.truncated format code"%z" % [1](operator)Internal Error+ Java stack traceUnrecognized conversion type: zUnrecognised conversion type: zunrecognized conversion type: zTest plan
./mill 'sjsonnet.jvm[3.3.8]'.test— all 29 tests passerror.format_invalid_conversion.jsonnet— verifies clean error message for%zerror.format_truncated.jsonnet— verifies clean error message for trailing%format_error_messages.jsonnet— verifies format operations still work correctly%operator andstd.formatboth produce clean errors