Skip to content

Commit 68db0ef

Browse files
committed
A potential fix for messy error boxes by RuntimeError
Signed-off-by: dafnapension <dafnashein@yahoo.com>
1 parent 92827a3 commit 68db0ef

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

src/unitxt/error_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,7 @@ def error_context(context_object: Any = None, **context):
298298
try:
299299
yield
300300
except Exception as e:
301+
if e.__class__.__name__ == "KeyError":
302+
e = RuntimeError(e.__class__.__name__ + ": '" + e.args[0] + "'")
301303
_add_context_to_exception(e, context_object, **context)
302-
raise
304+
raise e

tests/library/test_error_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TestProcessor:
4747

4848
processor = TestProcessor()
4949

50-
with self.assertRaises(KeyError) as cm:
50+
with self.assertRaises(RuntimeError) as cm:
5151
with error_context(processor):
5252
raise KeyError("Missing key")
5353

@@ -186,12 +186,12 @@ class TestProcessor:
186186

187187
def test_error_context_without_object(self):
188188
"""Test error_context without a context object."""
189-
with self.assertRaises(KeyError) as cm:
189+
with self.assertRaises(RuntimeError) as cm:
190190
with error_context(input_file="data.json", line_number=156):
191191
raise KeyError("Missing field")
192192

193193
error = cm.exception
194-
self.assertIsInstance(error, KeyError)
194+
self.assertIsInstance(error, RuntimeError)
195195
self.assertIsNone(error.context_object)
196196
# Context now includes version info plus the specified context
197197
self.assertIn("Unitxt", error.context)

0 commit comments

Comments
 (0)