mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 18:19:34 +00:00
Errors handler
This commit is contained in:
parent
569a9c807c
commit
9e673998f0
9 changed files with 150 additions and 3 deletions
|
|
@ -80,6 +80,9 @@ class MyMiddleware(BaseMiddleware):
|
|||
) -> Any:
|
||||
return "poll_answer"
|
||||
|
||||
async def on_pre_process_error(self, exception: Exception, data: Dict[str, Any]) -> Any:
|
||||
return "error"
|
||||
|
||||
async def on_process_update(self, update: Update, data: Dict[str, Any]) -> Any:
|
||||
return "update"
|
||||
|
||||
|
|
@ -130,6 +133,9 @@ class MyMiddleware(BaseMiddleware):
|
|||
async def on_process_poll_answer(self, poll_answer: PollAnswer, data: Dict[str, Any]) -> Any:
|
||||
return "poll_answer"
|
||||
|
||||
async def on_process_error(self, exception: Exception, data: Dict[str, Any]) -> Any:
|
||||
return "error"
|
||||
|
||||
async def on_post_process_update(
|
||||
self, update: Update, data: Dict[str, Any], result: Any
|
||||
) -> Any:
|
||||
|
|
@ -188,6 +194,11 @@ class MyMiddleware(BaseMiddleware):
|
|||
) -> Any:
|
||||
return "poll_answer"
|
||||
|
||||
async def on_post_process_error(
|
||||
self, exception: Exception, data: Dict[str, Any], result: Any
|
||||
) -> Any:
|
||||
return "error"
|
||||
|
||||
|
||||
UPDATE = Update(update_id=42)
|
||||
MESSAGE = Message(message_id=42, date=datetime.datetime.now(), chat=Chat(id=42, type="private"))
|
||||
|
|
@ -206,7 +217,12 @@ class TestBaseMiddleware:
|
|||
)
|
||||
@pytest.mark.parametrize(
|
||||
"event_name,event",
|
||||
[["update", UPDATE], ["message", MESSAGE], ["poll_answer", POLL_ANSWER],],
|
||||
[
|
||||
["update", UPDATE],
|
||||
["message", MESSAGE],
|
||||
["poll_answer", POLL_ANSWER],
|
||||
["error", Exception("KABOOM")],
|
||||
],
|
||||
)
|
||||
async def test_trigger(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ from aiogram.api.types import (
|
|||
Update,
|
||||
User,
|
||||
)
|
||||
from aiogram.dispatcher.event.observer import SkipHandler
|
||||
from aiogram.dispatcher.event.observer import SkipHandler, skip
|
||||
from aiogram.dispatcher.middlewares.base import BaseMiddleware
|
||||
from aiogram.dispatcher.router import Router
|
||||
from aiogram.utils.warnings import CodeHasNoEffect
|
||||
|
|
@ -416,3 +416,65 @@ class TestRouter:
|
|||
assert isinstance(middleware, BaseMiddleware)
|
||||
assert middleware.configured
|
||||
assert middleware.manager == router.middleware
|
||||
|
||||
def test_skip(self):
|
||||
with pytest.raises(SkipHandler):
|
||||
skip()
|
||||
with pytest.raises(SkipHandler, match="KABOOM"):
|
||||
skip("KABOOM")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exception_handler_catch_exceptions(self):
|
||||
root_router = Router()
|
||||
router = Router()
|
||||
root_router.include_router(router)
|
||||
|
||||
@router.message_handler()
|
||||
async def message_handler(message: Message):
|
||||
raise Exception("KABOOM")
|
||||
|
||||
update = Update(
|
||||
update_id=42,
|
||||
message=Message(
|
||||
message_id=42,
|
||||
date=datetime.datetime.now(),
|
||||
text="test",
|
||||
chat=Chat(id=42, type="private"),
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
),
|
||||
)
|
||||
with pytest.raises(Exception, match="KABOOM"):
|
||||
await root_router.listen_update(
|
||||
update_type="message",
|
||||
update=update,
|
||||
event=update.message,
|
||||
from_user=update.message.from_user,
|
||||
chat=update.message.chat,
|
||||
)
|
||||
|
||||
@root_router.errors_handler()
|
||||
async def root_error_handler(exception: Exception):
|
||||
return exception
|
||||
|
||||
response = await root_router.listen_update(
|
||||
update_type="message",
|
||||
update=update,
|
||||
event=update.message,
|
||||
from_user=update.message.from_user,
|
||||
chat=update.message.chat,
|
||||
)
|
||||
assert isinstance(response, Exception)
|
||||
assert str(response) == "KABOOM"
|
||||
|
||||
@router.errors_handler()
|
||||
async def error_handler(exception: Exception):
|
||||
return "KABOOM"
|
||||
|
||||
response = await root_router.listen_update(
|
||||
update_type="message",
|
||||
update=update,
|
||||
event=update.message,
|
||||
from_user=update.message.from_user,
|
||||
chat=update.message.chat,
|
||||
)
|
||||
assert response == "KABOOM"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue