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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
"""Error endpoint module.
This is not an actual endpoint, but rather a module that is used
to handle errors from any endpoint.
"""
from __future__ import annotations
from enum import IntEnum
import logging
from typing import Any
from asusrouter.const import UNKNOWN_MEMBER, HTTPStatus
from asusrouter.error import (
AsusRouterAccessError,
AsusRouterLogoutError,
AsusRouterRequestFormatError,
)
from asusrouter.modules.endpoint import EndpointType
from asusrouter.tools.readers import read_json_content
_LOGGER = logging.getLogger(__name__)
class AccessError(IntEnum):
"""Access error enum."""
UNKNOWN = UNKNOWN_MEMBER
SUCCESS = HTTPStatus.OK
NO_ERROR = 0
AUTHORIZATION = 2
CREDENTIALS = 3
TRY_AGAIN = 7
LOGOUT = 8
ANOTHER = 9
CAPTCHA = 10
RESET_REQUIRED = 11
def handle_access_error(
endpoint: EndpointType, status: Any, headers: Any, content: Any
) -> None:
"""Handle access errors."""
# Read the page as json
message = read_json_content(content)
# Get error code
error_status = int(message.get("error_status") or UNKNOWN_MEMBER)
# Formatting errors
if error_status in (
HTTPStatus.JSON_BAD_FORMAT,
HTTPStatus.JSON_BAD_REQUEST,
):
raise AsusRouterRequestFormatError("JSON format error")
try:
error = AccessError(error_status)
except ValueError:
error = AccessError.UNKNOWN
# Success
if error == AccessError.SUCCESS:
return
_LOGGER.debug("Access error message: %s", message)
# Additional values
attributes: dict[str, Any] = {}
# Handle logout code (even though it's not an error)
if error == AccessError.LOGOUT:
raise AsusRouterLogoutError("Session is logged out")
# Try again later error
if error == AccessError.TRY_AGAIN:
timeout = message.get("remaining_lock_time")
if timeout:
attributes["timeout"] = int(timeout)
# Return
raise AsusRouterAccessError(
"Access error",
error,
attributes,
)
|