Skip to content

Commit

Permalink
IL-306 Exception record output
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixFehseTNG committed Apr 8, 2024
1 parent 3ec5e2f commit 03a64b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/intelligence_layer/core/tracer/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ def record_output(self, output: PydanticSerializable) -> None:
"""
...

def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
) -> None:
if exc_type is not None and exc_value is not None and _traceback is not None:
error_value = ErrorValue(
error_type=str(exc_type.__qualname__),
message=str(exc_value),
stack_trace=str(traceback.format_exc()),
)
self.record_output(error_value)
self.end()


TracerVar = TypeVar("TracerVar", bound=Tracer)

Expand Down
19 changes: 17 additions & 2 deletions tests/core/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def test_task_logs_error_value() -> None:
tracer = InMemoryTracer()

with pytest.raises(ValueError):
with tracer.task_span("failing task", None):
with tracer.span("failing task"):
raise ValueError("my bad, sorry")

assert isinstance(tracer.entries[0], InMemoryTaskSpan)
assert isinstance(tracer.entries[0], InMemorySpan)
assert isinstance(tracer.entries[0].entries[0], LogEntry)
error = tracer.entries[0].entries[0].value
assert isinstance(error, ErrorValue)
Expand All @@ -130,6 +130,21 @@ def test_task_logs_error_value() -> None:
assert error.stack_trace.startswith("Traceback")


def test_task_span_records_error_value() -> None:
tracer = InMemoryTracer()

with pytest.raises(ValueError):
with tracer.task_span("failing task", None):
raise ValueError("my bad, sorry")

assert isinstance(tracer.entries[0], InMemoryTaskSpan)
error = tracer.entries[0].output
assert isinstance(error, ErrorValue)
assert error.message == "my bad, sorry"
assert error.error_type == "ValueError"
assert error.stack_trace.startswith("Traceback")


def test_task_automatically_logs_input_and_output(
complete: Task[CompleteInput, CompleteOutput],
) -> None:
Expand Down

0 comments on commit 03a64b0

Please sign in to comment.