Skip to content

Commit 1918bf7

Browse files
committed
chore: cleanup capture exception impl
1 parent 0adb534 commit 1918bf7

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

src/judgeval/common/tracer.py

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,17 @@ def save(self, overwrite: bool = False) -> Tuple[str, dict]:
685685
def delete(self):
686686
return self.trace_manager_client.delete_trace(self.trace_id)
687687

688-
688+
def _capture_exception_for_trace(current_trace: Optional['TraceClient'], exc_info: Tuple[Optional[type], Optional[BaseException], Optional[types.TracebackType]]):
689+
if not current_trace:
690+
return
691+
692+
exc_type, exc_value, exc_traceback_obj = exc_info
693+
formatted_exception = {
694+
"type": exc_type.__name__ if exc_type else "UnknownExceptionType",
695+
"message": str(exc_value) if exc_value else "No exception message",
696+
"traceback": traceback.format_tb(exc_traceback_obj) if exc_traceback_obj else []
697+
}
698+
current_trace.record_error(formatted_exception)
689699
class _DeepTracer:
690700
_instance: Optional["_DeepTracer"] = None
691701
_lock: threading.Lock = threading.Lock()
@@ -877,14 +887,11 @@ def _trace(self, frame: types.FrameType, event: str, arg: Any):
877887
current_span_var.reset(frame.f_locals["_judgment_span_token"])
878888

879889
elif event == "exception":
880-
exc_type, exc_value, exc_traceback = arg
881-
formatted_exception = {
882-
"type": exc_type.__name__,
883-
"message": str(exc_value),
884-
"traceback": traceback.format_tb(exc_traceback)
885-
}
886-
current_trace = current_trace_var.get()
887-
current_trace.record_error(formatted_exception)
890+
exc_type = arg[0]
891+
if issubclass(exc_type, (StopIteration, StopAsyncIteration, GeneratorExit)):
892+
return
893+
_capture_exception_for_trace(current_trace, arg)
894+
888895

889896
return self._trace
890897

@@ -1163,14 +1170,7 @@ async def async_wrapper(*args, **kwargs):
11631170
try:
11641171
result = await func(*args, **kwargs)
11651172
except Exception as e:
1166-
exc_type, exc_value, exc_traceback = sys.exc_info()
1167-
formatted_exception = {
1168-
"type": exc_type.__name__,
1169-
"message": str(exc_value),
1170-
"traceback": traceback.format_tb(exc_traceback)
1171-
}
1172-
current_trace = current_trace_var.get()
1173-
current_trace.record_error(formatted_exception)
1173+
_capture_exception_for_trace(current_trace, sys.exc_info())
11741174
raise e
11751175

11761176
# Record output
@@ -1195,14 +1195,7 @@ async def async_wrapper(*args, **kwargs):
11951195
try:
11961196
result = await func(*args, **kwargs)
11971197
except Exception as e:
1198-
exc_type, exc_value, exc_traceback = sys.exc_info()
1199-
formatted_exception = {
1200-
"type": exc_type.__name__,
1201-
"message": str(exc_value),
1202-
"traceback": traceback.format_tb(exc_traceback)
1203-
}
1204-
current_trace = current_trace_var.get()
1205-
current_trace.record_error(formatted_exception)
1198+
_capture_exception_for_trace(current_trace, sys.exc_info())
12061199
raise e
12071200

12081201
span.record_output(result)
@@ -1252,14 +1245,7 @@ def wrapper(*args, **kwargs):
12521245
try:
12531246
result = func(*args, **kwargs)
12541247
except Exception as e:
1255-
exc_type, exc_value, exc_traceback = sys.exc_info()
1256-
formatted_exception = {
1257-
"type": exc_type.__name__,
1258-
"message": str(exc_value),
1259-
"traceback": traceback.format_tb(exc_traceback)
1260-
}
1261-
current_trace = current_trace_var.get()
1262-
current_trace.record_error(formatted_exception)
1248+
_capture_exception_for_trace(current_trace, sys.exc_info())
12631249
raise e
12641250

12651251
# Record output
@@ -1285,14 +1271,7 @@ def wrapper(*args, **kwargs):
12851271
try:
12861272
result = func(*args, **kwargs)
12871273
except Exception as e:
1288-
exc_type, exc_value, exc_traceback = sys.exc_info()
1289-
formatted_exception = {
1290-
"type": exc_type.__name__,
1291-
"message": str(exc_value),
1292-
"traceback": traceback.format_tb(exc_traceback)
1293-
}
1294-
current_trace = current_trace_var.get()
1295-
current_trace.record_error(formatted_exception)
1274+
_capture_exception_for_trace(current_trace, sys.exc_info())
12961275
raise e
12971276

12981277
span.record_output(result)

0 commit comments

Comments
 (0)