mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-09 17:33:44 +00:00
Implemented error handler in dispatcher
(requested by @Oleg_Oleg_Oleg)
This commit is contained in:
parent
bac44d58a6
commit
534dea6de7
2 changed files with 104 additions and 53 deletions
|
|
@ -3,7 +3,8 @@ import functools
|
||||||
import logging
|
import logging
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
from .filters import CommandsFilter, ContentTypeFilter, RegexpFilter, USER_STATE, generate_default_filters
|
from .filters import CommandsFilter, ContentTypeFilter, RegexpFilter, USER_STATE, generate_default_filters, \
|
||||||
|
ExceptionsFilter
|
||||||
from .handler import Handler
|
from .handler import Handler
|
||||||
from .storage import BaseStorage, DisabledStorage, FSMContext
|
from .storage import BaseStorage, DisabledStorage, FSMContext
|
||||||
from .webhook import BaseResponse
|
from .webhook import BaseResponse
|
||||||
|
|
@ -52,6 +53,8 @@ class Dispatcher:
|
||||||
|
|
||||||
self.updates_handler.register(self.process_update)
|
self.updates_handler.register(self.process_update)
|
||||||
|
|
||||||
|
self.errors_handlers = Handler(self, once=False)
|
||||||
|
|
||||||
self._pooling = False
|
self._pooling = False
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
|
@ -93,58 +96,64 @@ class Dispatcher:
|
||||||
:param update:
|
:param update:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.last_update_id = update.update_id
|
try:
|
||||||
has_context = context.check_configured()
|
self.last_update_id = update.update_id
|
||||||
if update.message:
|
has_context = context.check_configured()
|
||||||
if has_context:
|
if update.message:
|
||||||
state = self.storage.get_state(chat=update.message.chat.id,
|
if has_context:
|
||||||
user=update.message.from_user.id)
|
state = self.storage.get_state(chat=update.message.chat.id,
|
||||||
context.set_value(USER_STATE, await state)
|
user=update.message.from_user.id)
|
||||||
return await self.message_handlers.notify(update.message)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.edited_message:
|
return await self.message_handlers.notify(update.message)
|
||||||
if has_context:
|
if update.edited_message:
|
||||||
state = self.storage.get_state(chat=update.edited_message.chat.id,
|
if has_context:
|
||||||
user=update.edited_message.from_user.id)
|
state = self.storage.get_state(chat=update.edited_message.chat.id,
|
||||||
context.set_value(USER_STATE, await state)
|
user=update.edited_message.from_user.id)
|
||||||
return await self.edited_message_handlers.notify(update.edited_message)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.channel_post:
|
return await self.edited_message_handlers.notify(update.edited_message)
|
||||||
if has_context:
|
if update.channel_post:
|
||||||
state = self.storage.get_state(chat=update.message.chat.id,
|
if has_context:
|
||||||
user=update.message.from_user.id)
|
state = self.storage.get_state(chat=update.message.chat.id,
|
||||||
context.set_value(USER_STATE, await state)
|
user=update.message.from_user.id)
|
||||||
return await self.channel_post_handlers.notify(update.channel_post)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.edited_channel_post:
|
return await self.channel_post_handlers.notify(update.channel_post)
|
||||||
if has_context:
|
if update.edited_channel_post:
|
||||||
state = self.storage.get_state(chat=update.edited_channel_post.chat.id,
|
if has_context:
|
||||||
user=update.edited_channel_post.from_user.id)
|
state = self.storage.get_state(chat=update.edited_channel_post.chat.id,
|
||||||
context.set_value(USER_STATE, await state)
|
user=update.edited_channel_post.from_user.id)
|
||||||
return await self.edited_channel_post_handlers.notify(update.edited_channel_post)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.inline_query:
|
return await self.edited_channel_post_handlers.notify(update.edited_channel_post)
|
||||||
if has_context:
|
if update.inline_query:
|
||||||
state = self.storage.get_state(user=update.inline_query.from_user.id)
|
if has_context:
|
||||||
context.set_value(USER_STATE, await state)
|
state = self.storage.get_state(user=update.inline_query.from_user.id)
|
||||||
return await self.inline_query_handlers.notify(update.inline_query)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.chosen_inline_result:
|
return await self.inline_query_handlers.notify(update.inline_query)
|
||||||
if has_context:
|
if update.chosen_inline_result:
|
||||||
state = self.storage.get_state(user=update.chosen_inline_result.from_user.id)
|
if has_context:
|
||||||
context.set_value(USER_STATE, await state)
|
state = self.storage.get_state(user=update.chosen_inline_result.from_user.id)
|
||||||
return await self.chosen_inline_result_handlers.notify(update.chosen_inline_result)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.callback_query:
|
return await self.chosen_inline_result_handlers.notify(update.chosen_inline_result)
|
||||||
if has_context:
|
if update.callback_query:
|
||||||
state = self.storage.get_state(chat=update.callback_query.message.chat.id,
|
if has_context:
|
||||||
user=update.callback_query.from_user.id)
|
state = self.storage.get_state(chat=update.callback_query.message.chat.id,
|
||||||
context.set_value(USER_STATE, await state)
|
user=update.callback_query.from_user.id)
|
||||||
return await self.callback_query_handlers.notify(update.callback_query)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.shipping_query:
|
return await self.callback_query_handlers.notify(update.callback_query)
|
||||||
if has_context:
|
if update.shipping_query:
|
||||||
state = self.storage.get_state(user=update.shipping_query.from_user.id)
|
if has_context:
|
||||||
context.set_value(USER_STATE, await state)
|
state = self.storage.get_state(user=update.shipping_query.from_user.id)
|
||||||
return await self.shipping_query_handlers.notify(update.shipping_query)
|
context.set_value(USER_STATE, await state)
|
||||||
if update.pre_checkout_query:
|
return await self.shipping_query_handlers.notify(update.shipping_query)
|
||||||
if has_context:
|
if update.pre_checkout_query:
|
||||||
state = self.storage.get_state(user=update.pre_checkout_query.from_user.id)
|
if has_context:
|
||||||
context.set_value(USER_STATE, await state)
|
state = self.storage.get_state(user=update.pre_checkout_query.from_user.id)
|
||||||
return await self.pre_checkout_query_handlers.notify(update.pre_checkout_query)
|
context.set_value(USER_STATE, await state)
|
||||||
|
return await self.pre_checkout_query_handlers.notify(update.pre_checkout_query)
|
||||||
|
except Exception as e:
|
||||||
|
err = await self.errors_handlers.notify(self, update, e)
|
||||||
|
if err:
|
||||||
|
return err
|
||||||
|
raise
|
||||||
|
|
||||||
async def start_pooling(self, timeout=20, relax=0.1, limit=None):
|
async def start_pooling(self, timeout=20, relax=0.1, limit=None):
|
||||||
"""
|
"""
|
||||||
|
|
@ -746,6 +755,35 @@ class Dispatcher:
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
def register_errors_handler(self, callback, *, func=None, exception=None):
|
||||||
|
"""
|
||||||
|
Register errors handler
|
||||||
|
|
||||||
|
:param callback:
|
||||||
|
:param func:
|
||||||
|
:param exception: you can make handler for specific errors type
|
||||||
|
"""
|
||||||
|
filters_set = []
|
||||||
|
if func is not None:
|
||||||
|
filters_set.append(func)
|
||||||
|
if exception is not None:
|
||||||
|
filters_set.append(ExceptionsFilter(exception))
|
||||||
|
self.errors_handlers.register(callback, filters_set)
|
||||||
|
|
||||||
|
def errors_handler(self, *, func=None, exception=None):
|
||||||
|
"""
|
||||||
|
Decorator for registering errors handler
|
||||||
|
|
||||||
|
:param func:
|
||||||
|
:param exception: you can make handler for specific errors type
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
def decorator(callback):
|
||||||
|
self.register_errors_handler(callback, func=func, exception=exception)
|
||||||
|
return callback
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
def current_state(self, *,
|
def current_state(self, *,
|
||||||
chat: typing.Union[str, int, None] = None,
|
chat: typing.Union[str, int, None] = None,
|
||||||
user: typing.Union[str, int, None] = None) -> FSMContext:
|
user: typing.Union[str, int, None] = None) -> FSMContext:
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,19 @@ class StatesListFilter(StateFilter):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class ExceptionsFilter(Filter):
|
||||||
|
def __init__(self, exception):
|
||||||
|
self.exception = exception
|
||||||
|
|
||||||
|
def check(self, dispatcher, update, exception):
|
||||||
|
try:
|
||||||
|
raise exception
|
||||||
|
except self.exception:
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def generate_default_filters(dispatcher, *args, **kwargs):
|
def generate_default_filters(dispatcher, *args, **kwargs):
|
||||||
filters_set = []
|
filters_set = []
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue