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:
18
oxi/node.py
18
oxi/node.py
@@ -1,5 +1,9 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from requests import HTTPError
|
||||
|
||||
from oxi.exception import OxiAPIError
|
||||
|
||||
from .view import NodeView
|
||||
|
||||
|
||||
@@ -7,18 +11,20 @@ if TYPE_CHECKING:
|
||||
from requests import Session
|
||||
|
||||
|
||||
# TODO: Add type hints
|
||||
class Node:
|
||||
def __init__(self, session: "Session", base_url: str):
|
||||
self._session = session
|
||||
self._base_url = base_url
|
||||
|
||||
def __call__(self, name: str) -> NodeView:
|
||||
url = f"{self._base_url}/node/show/{name}"
|
||||
if not url.endswith(".json"):
|
||||
url += ".json"
|
||||
response = self._session.get(url)
|
||||
response.raise_for_status()
|
||||
try:
|
||||
url = f"{self._base_url}/node/show/{name}"
|
||||
if not url.endswith(".json"):
|
||||
url += ".json"
|
||||
response = self._session.get(url)
|
||||
response.raise_for_status()
|
||||
except HTTPError as e:
|
||||
raise OxiAPIError.from_http_error(e, context=f"Node {name}") from e
|
||||
return NodeView(
|
||||
session=self._session, base_url=self._base_url, data=response.json()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user