From dc4424b7d27f11a022ae9ffc5e88d7dd74832a50 Mon Sep 17 00:00:00 2001 From: He-Pin Date: Thu, 18 Jun 2026 18:08:40 +0800 Subject: [PATCH] fix: error expression produces clean message for function values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Motivation: `error function(x) x` produced a misleading message like "Couldn't manifest function with params [x]" instead of a clear error about attempting to serialize a function. Modification: Add a Val.Func case to Evaluator.materializeError so that error expressions with function values produce "Couldn't manifest function as JSON" — aligned with go-jsonnet's "couldn't manifest function as JSON" and jrsonnet's "tried to manifest function". Result: `error function(x) x` now produces a clean, implementation-consistent error message instead of leaking internal parameter details. | Impl | Error message | |------|--------------| | go-jsonnet v0.22.0 | RUNTIME ERROR: couldn't manifest function as JSON | | jrsonnet 0.5.0-pre99 | runtime error: tried to manifest function | | sjsonnet (before) | Couldn't manifest function with params [x] | | sjsonnet (after) | Couldn't manifest function as JSON | --- sjsonnet/src/sjsonnet/Evaluator.scala | 1 + .../resources/go_test_suite/error_function_fail.jsonnet.golden | 2 +- .../test/resources/new_test_suite/error.error_function.jsonnet | 1 + .../new_test_suite/error.error_function.jsonnet.golden | 2 ++ 4 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet create mode 100644 sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet.golden diff --git a/sjsonnet/src/sjsonnet/Evaluator.scala b/sjsonnet/src/sjsonnet/Evaluator.scala index e41deda5d..908791eeb 100644 --- a/sjsonnet/src/sjsonnet/Evaluator.scala +++ b/sjsonnet/src/sjsonnet/Evaluator.scala @@ -810,6 +810,7 @@ class Evaluator( protected def materializeError(value: Val): String = value match { case Val.Str(_, s) => s + case _: Val.Func => "Couldn't manifest function as JSON" case r => Materializer.stringify(r) } diff --git a/sjsonnet/test/resources/go_test_suite/error_function_fail.jsonnet.golden b/sjsonnet/test/resources/go_test_suite/error_function_fail.jsonnet.golden index a83dbafd1..730007714 100644 --- a/sjsonnet/test/resources/go_test_suite/error_function_fail.jsonnet.golden +++ b/sjsonnet/test/resources/go_test_suite/error_function_fail.jsonnet.golden @@ -1,3 +1,3 @@ -sjsonnet.Error: Couldn't manifest function with params [x] +sjsonnet.Error: Couldn't manifest function as JSON at [].(error_function_fail.jsonnet:1:1) diff --git a/sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet b/sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet new file mode 100644 index 000000000..b0166b396 --- /dev/null +++ b/sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet @@ -0,0 +1 @@ +error function(x) x diff --git a/sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet.golden b/sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet.golden new file mode 100644 index 000000000..594635b5b --- /dev/null +++ b/sjsonnet/test/resources/new_test_suite/error.error_function.jsonnet.golden @@ -0,0 +1,2 @@ +sjsonnet.Error: Couldn't manifest function as JSON + at [].(error.error_function.jsonnet:1:1)