Skip to content

Commit 7bd98be

Browse files
feat: capture unhandled errors in reporter
1 parent 9a475c5 commit 7bd98be

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

src/askui/agent_base.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import time
3+
import traceback as _traceback
34
import types
45
from pathlib import Path
56
from typing import Annotated, Literal, Optional, Type, overload
@@ -871,9 +872,11 @@ def _wait_for_disappear(
871872

872873
@telemetry.record_call()
873874
def close(self) -> None:
874-
if self._agent_os is not None:
875-
self._agent_os.disconnect()
876-
self._reporter.generate()
875+
try:
876+
self._reporter.generate()
877+
finally:
878+
if self._agent_os is not None:
879+
self._agent_os.disconnect()
877880

878881
@telemetry.record_call()
879882
def open(self) -> None:
@@ -892,8 +895,23 @@ def __exit__(
892895
exc_value: BaseException | None,
893896
traceback: types.TracebackType | None,
894897
) -> None:
898+
if exc_value is not None:
899+
self._report_unhandled_error(exc_value, traceback)
895900
self.close()
896901

902+
def _report_unhandled_error(
903+
self,
904+
exc_value: BaseException,
905+
tb: types.TracebackType | None,
906+
) -> None:
907+
try:
908+
formatted = "".join(
909+
_traceback.format_exception(type(exc_value), exc_value, tb)
910+
)
911+
self._reporter.add_message("Error", formatted)
912+
except Exception: # noqa: BLE001
913+
logger.exception("Failed to add unhandled error to reporter")
914+
897915
@staticmethod
898916
def get_default_tools() -> list[Tool]:
899917
return []

0 commit comments

Comments
 (0)