Skip to content

Commit 39c4003

Browse files
committed
fixup: apply code suggestions on the manager
1 parent bd62ce8 commit 39c4003

File tree

1 file changed

+15
-20
lines changed
  • pydis_core/utils/error_handling/commands

1 file changed

+15
-20
lines changed

pydis_core/utils/error_handling/commands/manager.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,44 @@
1313
class CommandErrorManager:
1414
"""A class that registers error handlers and handles all command related errors."""
1515

16-
def __init__(self, default: AbstractCommandErrorHandler, handlers: list[AbstractCommandErrorHandler] | None = None):
17-
self.handlers = handlers or []
18-
self.registered_handlers = set(type(handler).__name__ for handler in self.handlers)
19-
self.default = default
16+
def __init__(self, default: AbstractCommandErrorHandler):
17+
self._handlers = []
18+
self._registered_handlers = set()
19+
self._default = default
2020

2121
async def handle_error(
2222
self,
2323
error: Exception,
2424
context_or_interaction: Context | Interaction
2525
) -> None:
2626
"""
27-
Handle a discord exception.
27+
Handle a Discord exception.
2828
29-
This works simply by iterating over the available handlers, and chooses the one capable of handling the error.
30-
31-
Instead of having some priority system to distinguish the execution order of handlers, we will
32-
let the `should_handle_error` do the job.
33-
34-
The default needs to always be last, since the container can either be initialized with the handlers, or
35-
we can add handlers using the `register_handler` function that we don't know when it will be invoked, if ever.
29+
Iterate through available handlers by registration order, and choose the first one capable of handling
30+
the error as determined by `should_handle_error`; there is no priority system.
3631
"""
37-
for handler in self.handlers + [self.default]:
32+
for handler in self._handlers + [self._default]:
3833
if await handler.should_handle_error(error):
39-
callback = self._determine_callback(handler, context_or_interaction)
34+
callback = self._get_callback(handler, context_or_interaction)
4035
await callback(context_or_interaction, error)
4136
break
4237

4338
def register_handler(self, handler: AbstractCommandErrorHandler) -> None:
4439
"""Register a command error handler."""
4540
handler_name = type(handler).__name__
46-
if handler_name in self.registered_handlers:
47-
log.info(f"Skipping registration of command error handler '{handler_name}' as it's already registered.")
41+
if handler_name in self._registered_handlers:
42+
log.info(f"Skipping registration of command error handler {handler_name!r} as it's already registered.")
4843
return
4944

50-
self.handlers.append(handler)
51-
self.registered_handlers.add(type(handler).__name__)
45+
self._handlers.append(handler)
46+
self._registered_handlers.add(handler_name)
5247

5348
@staticmethod
54-
def _determine_callback(
49+
def _get_callback(
5550
handler: AbstractCommandErrorHandler,
5651
context_or_interaction: Context | Interaction
5752
) -> Callable[[Context, Exception], NoReturn] | Callable[[Interaction, Exception], NoReturn] | None:
58-
"""Determines the handling callback that will be used."""
53+
"""Get the handling callback that will be used."""
5954
if isinstance(context_or_interaction, Context):
6055
return handler.handle_text_command_error
6156
return handler.handle_app_command_error

0 commit comments

Comments
 (0)