Enhance error handling in OxiAPI and Node classes

- Updated the `reload` method in the `OxiAPI` class to catch `HTTPError` exceptions and raise a custom `OxiAPIError` with context.
- Improved the `__call__` method in the `Node` class to handle `HTTPError` exceptions similarly, providing context-specific error messages.
- Introduced a new class method `from_http_error` in `OxiAPIError` for standardized error message generation based on HTTP status codes.
This commit is contained in:
IluaAir
2026-03-26 20:10:05 +03:00
parent 1cc225917e
commit 0b92e342e5
3 changed files with 42 additions and 10 deletions

View File

@@ -1,4 +1,16 @@
from typing import Optional
from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
from requests import HTTPError
_STATUS_MESSAGES: dict[int, str] = {
401: "Unauthorized",
403: "Forbidden",
500: "Internal Server Error",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
}
class OxiAPIError(Exception):
@@ -10,3 +22,13 @@ class OxiAPIError(Exception):
if self.status_code is not None:
return f"OxiAPIError: {self.args[0]} (HTTP {self.status_code})"
return f"OxiAPIError: {self.args[0]}"
@classmethod
def from_http_error(cls, e: "HTTPError", context: str = "") -> "OxiAPIError":
status = e.response.status_code
if status == 404:
message = f"{context} not found" if context else "Not found"
else:
base = _STATUS_MESSAGES.get(status) or e.response.reason or f"HTTP {status}"
message = f"{context}: {base}" if context else base
return cls(message, status)