summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-02 10:52:39 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-02 10:52:39 +0000
commit37c6f8a5cbb2cfc4cd9726ebc82c1ced7c7b2604 (patch)
treea9360a5d1c8664c2f6e349e0cd2736965e7c7685
parent76525df1ba37edf8dda6450a269f27e349d41613 (diff)
Handle non-string errors with constant
-rw-r--r--jq.pyx23
-rw-r--r--tests/jq_tests.py10
2 files changed, 24 insertions, 9 deletions
diff --git a/jq.pyx b/jq.pyx
index d8ebffa..c0a9c09 100644
--- a/jq.pyx
+++ b/jq.pyx
@@ -174,19 +174,24 @@ cdef jq_state* _compile(object program_bytes, object args) except NULL:
cdef void _store_error(void* store_ptr, jv error) noexcept:
- # TODO: handle errors not of JV_KIND_STRING
cdef _ErrorStore store = <_ErrorStore>store_ptr
- if jv_get_kind(error) == JV_KIND_STRING:
- try:
- error_string = jv_string_to_py_string(error)
- except:
- error_string = "Internal error"
- store.store_error(error_string)
+ error_string = _jq_error_to_py_string(error)
+ store.store_error(error_string)
jv_free(error)
+cdef unicode _jq_error_to_py_string(jv error) noexcept:
+ 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)"
+
+
cdef class _ErrorStore(object):
cdef object _errors
@@ -361,7 +366,7 @@ cdef class _ResultIterator(object):
return _jv_to_python(result)
elif jv_invalid_has_msg(jv_copy(result)):
error_message = jv_invalid_get_msg(result)
- message = jv_string_to_py_string(error_message)
+ message = _jq_error_to_py_string(error_message)
jv_free(error_message)
raise ValueError(message)
else:
@@ -394,7 +399,7 @@ cdef class _ResultIterator(object):
return value
elif jv_invalid_has_msg(jv_copy(value)):
error_message = jv_invalid_get_msg(value)
- message = jv_string_to_py_string(error_message)
+ message = _jq_error_to_py_string(error_message)
jv_free(error_message)
raise ValueError(u"parse error: " + message)
else:
diff --git a/tests/jq_tests.py b/tests/jq_tests.py
index 81eb79b..69c37df 100644
--- a/tests/jq_tests.py
+++ b/tests/jq_tests.py
@@ -217,6 +217,16 @@ 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():
+ program = jq.compile("error")
+ try:
+ program.input(1).all()
+ assert False, "Expected error"
+ except ValueError as error:
+ expected_error_str = "(not a string)"
+ assert_equal(str(error), expected_error_str)
+
+
def test_errors_do_not_leak_between_transformations():
program = jq.compile(".x")
try: