Skip to content

Commit 2528241

Browse files
authored
fix: std.parseJson reports truncated input cleanly (#1042)
## Motivation `std.parseJson` should report incomplete JSON input as a Jsonnet runtime error instead of leaking an internal ujson exception. Before this change, empty or truncated JSON strings produced `[std.parseJson] Internal Error`, which made the user-facing failure look like an interpreter bug. ## Modification - Catch `ujson.IncompleteParseException` in `std.parseJson`. - Report `Invalid JSON: unexpected end of JSON input` through the existing `Error.fail` path. - Add regression goldens for empty input and truncated object input. ## Result | Case | go-jsonnet v0.22.0 | jrsonnet 0.5.0-pre99 | sjsonnet before | sjsonnet after | | --- | --- | --- | --- | --- | | `std.parseJson("")` | `failed to parse JSON: unexpected end of JSON input` | `failed to parse json: EOF while parsing a value` | `[std.parseJson] Internal Error` | `[std.parseJson] Invalid JSON: unexpected end of JSON input` | | `std.parseJson('{"a":')` | `failed to parse JSON: unexpected end of JSON input` | `failed to parse json: EOF while parsing a value at line 1 column 5` | `[std.parseJson] Internal Error` | `[std.parseJson] Invalid JSON: unexpected end of JSON input` | The PR only changes `ManifestModule.scala` and the two regression tests with their goldens. ## Risks - This intentionally changes the exact error text for incomplete JSON input. - Other malformed JSON errors continue to use the existing parse-error path.
1 parent 628e0c1 commit 2528241

5 files changed

Lines changed: 6 additions & 0 deletions

File tree

sjsonnet/src/sjsonnet/stdlib/ManifestModule.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ object ManifestModule extends AbstractFunctionModule {
130130
} catch {
131131
case e: ujson.ParseException =>
132132
throw Error.fail("Invalid JSON: " + e.getMessage, pos)(ev)
133+
case _: ujson.IncompleteParseException =>
134+
throw Error.fail("Invalid JSON: unexpected end of JSON input", pos)(ev)
133135
}
134136
}
135137
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.parseJson("")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sjsonnet.Error: [std.parseJson] Invalid JSON: unexpected end of JSON input
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.parseJson('{"a":')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sjsonnet.Error: [std.parseJson] Invalid JSON: unexpected end of JSON input

0 commit comments

Comments
 (0)