summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-02 10:58:39 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-02 10:58:39 +0000
commitd671908e7f36fea2d128fc32779ff2d77cdb890b (patch)
treee9d93dc61d9cffff3903733982119a490c2b02ab
parent37c6f8a5cbb2cfc4cd9726ebc82c1ced7c7b2604 (diff)
Convert non-string errors into JSON text
-rw-r--r--jq.pyx4
-rw-r--r--tests/jq_tests.py6
2 files changed, 6 insertions, 4 deletions
diff --git a/jq.pyx b/jq.pyx
index c0a9c09..1b2056c 100644
--- a/jq.pyx
+++ b/jq.pyx
@@ -183,13 +183,15 @@ cdef void _store_error(void* store_ptr, jv error) noexcept:
cdef unicode _jq_error_to_py_string(jv error) noexcept:
+ error = jv_copy(error)
+
if jv_get_kind(error) == JV_KIND_STRING:
try:
return jv_string_to_py_string(error)
except:
return u"Internal error"
else:
- return u"(not a string)"
+ return json.dumps(_jv_to_python(error))
cdef class _ErrorStore(object):
diff --git a/tests/jq_tests.py b/tests/jq_tests.py
index 69c37df..c42c862 100644
--- a/tests/jq_tests.py
+++ b/tests/jq_tests.py
@@ -217,13 +217,13 @@ def test_value_error_is_raised_if_input_cannot_be_processed_by_program():
assert_equal(str(error), expected_error_str)
-def test_non_string_error_is_converted_to_generic_string():
+def test_non_string_error_is_converted_to_json_text():
program = jq.compile("error")
try:
- program.input(1).all()
+ program.input({"x": 1}).all()
assert False, "Expected error"
except ValueError as error:
- expected_error_str = "(not a string)"
+ expected_error_str = '{"x": 1}'
assert_equal(str(error), expected_error_str)