You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1.2 KiB

"""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