From ebdb3cbbea0b36bac418b833492c9b934a00e35b Mon Sep 17 00:00:00 2001 From: penguinboi Date: Sun, 12 Apr 2026 04:45:22 -0400 Subject: [PATCH] Improved fallback of error handler by setting Exception instead of using a fallback dict --- matrix/bot.py | 4 ---- matrix/registry.py | 10 +++++----- tests/test_bot.py | 4 +--- tests/test_registry.py | 10 ---------- 4 files changed, 6 insertions(+), 22 deletions(-) diff --git a/matrix/bot.py b/matrix/bot.py index 3dad2df..cdd5cd6 100644 --- a/matrix/bot.py +++ b/matrix/bot.py @@ -184,10 +184,6 @@ async def _on_error(self, error: Exception) -> None: await handler(error) return - if self._fallback_error_handler: - await self._fallback_error_handler(error) - return - await self._dispatch("on_error", error) async def on_command(self, _ctx: Context) -> None: diff --git a/matrix/registry.py b/matrix/registry.py index f8a6a66..7d60202 100644 --- a/matrix/registry.py +++ b/matrix/registry.py @@ -65,7 +65,6 @@ def __init__(self, name: str, prefix: Optional[str] = None): self._event_handlers: Dict[Type[Event], List[Callback]] = defaultdict(list) self._hook_handlers: Dict[str, List[Callback]] = defaultdict(list) - self._fallback_error_handler: Optional[ErrorCallback] = None self._error_handlers: Dict[type[Exception], ErrorCallback] = {} self._command_error_handlers: Dict[type[Exception], CommandErrorCallback] = {} @@ -378,14 +377,15 @@ async def on_any_error(error): ``` """ + if not exception: + exception = Exception + def wrapper(func: ErrorCallback) -> ErrorCallback: if not inspect.iscoroutinefunction(func): raise TypeError("Error handlers must be coroutines") - if exception: - self._error_handlers[exception] = func - else: - self._fallback_error_handler = func + self._error_handlers[exception] = func + logger.debug( "registered error handler '%s' on %s", func.__name__, diff --git a/tests/test_bot.py b/tests/test_bot.py index d98a27c..465a9e8 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -206,9 +206,7 @@ async def custom_error_handler(e): nonlocal called called = True - await bot._fallback_error_handler(Exception("test error")) - await bot.on_error(Exception("test error")) - + await bot._on_error(Exception("test error")) assert called, "Fallback error handler was not called" diff --git a/tests/test_registry.py b/tests/test_registry.py index ebf3a60..bb1eab9 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -297,16 +297,6 @@ async def on_value_error(error): assert registry._error_handlers[ValueError] is on_value_error -def test_register_generic_error_handler__expect_fallback_error_handler_set( - registry: Registry, -): - @registry.error() - async def on_any_error(error): - pass - - assert registry._fallback_error_handler is on_any_error - - def test_register_error_handler_with_non_coroutine__expect_type_error( registry: Registry, ):