|
13 | 13 | class CommandErrorManager:
|
14 | 14 | """A class that registers error handlers and handles all command related errors."""
|
15 | 15 |
|
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 |
20 | 20 |
|
21 | 21 | async def handle_error(
|
22 | 22 | self,
|
23 | 23 | error: Exception,
|
24 | 24 | context_or_interaction: Context | Interaction
|
25 | 25 | ) -> None:
|
26 | 26 | """
|
27 |
| - Handle a discord exception. |
| 27 | + Handle a Discord exception. |
28 | 28 |
|
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. |
36 | 31 | """
|
37 |
| - for handler in self.handlers + [self.default]: |
| 32 | + for handler in self._handlers + [self._default]: |
38 | 33 | 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) |
40 | 35 | await callback(context_or_interaction, error)
|
41 | 36 | break
|
42 | 37 |
|
43 | 38 | def register_handler(self, handler: AbstractCommandErrorHandler) -> None:
|
44 | 39 | """Register a command error handler."""
|
45 | 40 | 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.") |
48 | 43 | return
|
49 | 44 |
|
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) |
52 | 47 |
|
53 | 48 | @staticmethod
|
54 |
| - def _determine_callback( |
| 49 | + def _get_callback( |
55 | 50 | handler: AbstractCommandErrorHandler,
|
56 | 51 | context_or_interaction: Context | Interaction
|
57 | 52 | ) -> 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.""" |
59 | 54 | if isinstance(context_or_interaction, Context):
|
60 | 55 | return handler.handle_text_command_error
|
61 | 56 | return handler.handle_app_command_error
|
0 commit comments