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
|
"""Common errors handler."""
from logging import Logger
from socket import timeout
from rcon.exceptions import ConfigReadError
from rcon.exceptions import SessionTimeout
from rcon.exceptions import UserAbort
from rcon.exceptions import WrongPassword
__all__ = ["ErrorHandler"]
ERRORS = {
UserAbort: (1, None),
ConfigReadError: (2, None),
ConnectionRefusedError: (3, "Connection refused."),
(TimeoutError, timeout): (4, "Connection timed out."),
WrongPassword: (5, "Wrong password."),
SessionTimeout: (6, "Session timed out."),
}
class ErrorHandler:
"""Handles common errors and exits."""
__slots__ = ("logger", "exit_code")
def __init__(self, logger: Logger):
"""Set the logger."""
self.logger = logger
self.exit_code = 0
def __enter__(self):
return self
def __exit__(self, _, value: Exception, __):
"""Check for common errors and exit respectively."""
if value is None:
return True
for typ, (exit_code, message) in ERRORS.items():
if isinstance(value, typ):
self.exit_code = exit_code
if message:
self.logger.error(message)
return True
return None
|