unitxt icon indicating copy to clipboard operation
unitxt copied to clipboard

A potential fix for messy error box when Error is KeyError

Open dafnapension opened this issue 3 months ago • 5 comments

BEFORE: error_box_main.pdf

AFTER: error_box_fixed.pdf

dafnapension avatar Sep 01 '25 19:09 dafnapension

An example from test_error_context_with_context_object:

image

and from test_error_context_without_object:

image

dafnapension avatar Sep 01 '25 20:09 dafnapension

Hi @elronbandel , KeyError jumps off "by itself" (by python and not by unitxt) in cases like in here: a given string is not found in a dict (that the string is supposed to be one of its keys).

I changed to return an extension of KeyError that prints out its args not as a constant string, but obeying the \n -s. As an extension, it does return true for isinstance(e, KeyError) and hence no change in test_error_utils. It will affect a piece of code expecting this error in the form of if type(e) == KeyError:.

dafnapension avatar Sep 02 '25 21:09 dafnapension

So what does the error trace look like now? is it of the original error?

my suggestion was to wrap the problematic external code with:

try:
     # call to external code that raises key error
except KeyError as e:
    raise RuntimeError("Key was not found: {e}")

but this wrapping has to be write on top of the problematic code, so the error trace leads to the exact point of error.

elronbandel avatar Sep 03 '25 10:09 elronbandel

Hi @elronbandel , I attached the full notebook for you to see both traces. Are these close enough?

dafnapension avatar Sep 03 '25 21:09 dafnapension

HI @elronbandel , Effectively, I imbedded this piece of code that you suggested -- inside your error_context

I am not sure I understand whether you consider this "right on top of the problematic code,". If not - then forget it. I think that exact trace is most important. More than a well organized error box.

dafnapension avatar Sep 04 '25 08:09 dafnapension