Skip to content
This repository was archived by the owner on Feb 11, 2026. It is now read-only.

Commit eeb5d67

Browse files
committed
Добавлено управление обработкой ошибок в методах _safe_execute и _create_safe_task для улучшения обработки исключений.
1 parent 31b866a commit eeb5d67

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

src/pymax/interfaces.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@ async def _safe_execute(self, coro, *, context: str = "unknown") -> Any:
4949
try:
5050
return await coro
5151
except Exception as e:
52-
self.logger.error(f"Unhandled exception in {context}: {e}\n{traceback.format_exc()}")
52+
if self._on_error_handler:
53+
try:
54+
result = self._on_error_handler(e)
55+
if asyncio.iscoroutine(result):
56+
await result
57+
except Exception as eh:
58+
self.logger.exception(
59+
f"Error in on_error_handler while handling exception in {context}: {eh}\n{traceback.format_exc()}"
60+
)
61+
else:
62+
self.logger.exception(
63+
f"Unhandled exception in {context}: {e}\n{traceback.format_exc()}"
64+
)
5365

5466
def _create_safe_task(
5567
self, coro: Awaitable[Any], name: str | None = None
@@ -60,9 +72,20 @@ async def runner():
6072
except asyncio.CancelledError:
6173
raise
6274
except Exception as e:
63-
tb = traceback.format_exc()
64-
self.logger.error(f"Unhandled exception in task {name or coro}: {e}\n{tb}")
65-
raise
75+
if self._on_error_handler:
76+
try:
77+
result = self._on_error_handler(e)
78+
if asyncio.iscoroutine(result):
79+
await result
80+
except Exception as eh:
81+
tb = traceback.format_exc()
82+
self.logger.exception(
83+
f"Error in on_error_handler while handling exception in task {name or coro}: {eh}\n{tb}"
84+
)
85+
else:
86+
tb = traceback.format_exc()
87+
self.logger.exception(f"Unhandled exception in task {name or coro}: {e}\n{tb}")
88+
raise
6689

6790
task = asyncio.create_task(runner(), name=name)
6891
self._background_tasks.add(task)

0 commit comments

Comments
 (0)