Check error handler callback args to prevent cycled exceptions

This commit is contained in:
Oleg A 2018-11-15 02:56:53 +03:00
parent 61e0f3601d
commit fb9fb68130

View file

@ -4,6 +4,7 @@ import itertools
import logging
import time
import typing
from inspect import signature
from .filters import Command, ContentTypeFilter, ExceptionsFilter, FiltersFactory, FuncFilter, HashTag, Regexp, \
RegexpCommandsFilter, StateFilter, Text
@ -812,6 +813,12 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
:param exception: you can make handler for specific errors type
:param run_task: run callback in task (no wait results)
"""
# Check number of arguments of the callback, cause only two arguments accepted.
sig = signature(callback)
if len(sig.parameters) != 2:
raise RuntimeError('Errors handlers should accept only two arguments (current update and exception)')
filters_set = self.filters_factory.resolve(self.errors_handlers,
*custom_filters,
exception=exception,
@ -827,6 +834,7 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
:return:
"""
def decorator(callback):
self.register_errors_handler(self._wrap_async_task(callback, run_task),
*custom_filters, exception=exception, **kwargs)