Repository navigation
Expand file tree
/
Copy patherror.py
More file actions
58 lines (48 loc) · 1.47 KB
/
Copy patherror.py
File metadata and controls
58 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Error handling for event listeners.
"""
from __future__ import annotations
from collections.abc import Callable
from typing import Any
from .event import Event
class ErrorHandler:
"""
Handles errors that occur during event listener execution.
"""
def __init__(self) -> None:
self._handler: Callable[[BaseException, Event, Callable[..., Any]], None] | None = None
def set_handler(
self,
handler: Callable[[BaseException, Event, Callable[..., Any]], None],
) -> None:
"""
Set custom error handler.
Parameters
----------
handler : Callable[[BaseException, Event, Callable], None]
Error handler function
"""
self._handler = handler
def handle_error(
self, exc: BaseException, event: Event, listener: Callable[..., Any]
) -> None:
"""
Handle an error from a listener.
Parameters
----------
exc : BaseException
Exception that occurred
event : Event
Event being processed
listener : Callable
Listener that caused the error
"""
if self._handler:
self._handler(exc, event, listener)
else:
# Default behavior: log the error
import logging
logging.error(
f"Error in listener {getattr(listener, '__name__', repr(listener))} "
f"for event {event.type}: {exc}"
)