2021-04-09 14:31:35 +03:00
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
from typing import (
|
|
|
|
Awaitable,
|
|
|
|
Callable,
|
|
|
|
Optional,
|
|
|
|
)
|
|
|
|
|
2022-09-02 18:44:56 +03:00
|
|
|
from library.logging import error_log
|
2021-04-09 14:31:35 +03:00
|
|
|
from telethon import (
|
|
|
|
errors,
|
|
|
|
events,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
async def safe_execution(
|
2022-09-02 18:44:56 +03:00
|
|
|
error_log=error_log,
|
2021-04-09 14:31:35 +03:00
|
|
|
on_fail: Optional[Callable[[], Awaitable]] = None,
|
2022-09-02 18:44:56 +03:00
|
|
|
level=logging.WARNING,
|
2021-04-09 14:31:35 +03:00
|
|
|
):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
except events.StopPropagation:
|
|
|
|
raise
|
2022-09-02 18:44:56 +03:00
|
|
|
except errors.MessageNotModifiedError:
|
|
|
|
pass
|
2021-04-09 14:31:35 +03:00
|
|
|
except (
|
|
|
|
errors.UserIsBlockedError,
|
|
|
|
errors.QueryIdInvalidError,
|
|
|
|
errors.MessageDeleteForbiddenError,
|
|
|
|
errors.MessageIdInvalidError,
|
|
|
|
errors.ChatAdminRequiredError,
|
|
|
|
) as e:
|
2022-09-02 18:44:56 +03:00
|
|
|
error_log(e, level=level)
|
2021-04-09 14:31:35 +03:00
|
|
|
except Exception as e:
|
2022-09-02 18:44:56 +03:00
|
|
|
error_log(e, level=level)
|
2021-04-09 14:31:35 +03:00
|
|
|
traceback.print_exc()
|
|
|
|
if on_fail:
|
|
|
|
await on_fail()
|
|
|
|
except events.StopPropagation:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
2022-09-02 18:44:56 +03:00
|
|
|
error_log(e, level=level)
|