Skip to content
Snippets Groups Projects
Commit 07c601ee authored by Pierre Chanial's avatar Pierre Chanial
Browse files

Improve error handling of ESAP-DB server responses.

parent 701d2a59
Branches
No related tags found
No related merge requests found
"""This module provides helper functions for the ESAP client.""" """This module provides helper functions for the ESAP client."""
from typing import Type import json
from typing import Any, Type
from requests import Response from requests import Response
from tabulate import tabulate from tabulate import tabulate
...@@ -18,7 +19,12 @@ def raise_for_status(response: Response) -> None: ...@@ -18,7 +19,12 @@ def raise_for_status(response: Response) -> None:
else: else:
cls = ESAPServerError cls = ESAPServerError
error: Any
try:
error = response.json() error = response.json()
except json.JSONDecodeError:
error = response.text
if isinstance(error, dict) and 'detail' in error: if isinstance(error, dict) and 'detail' in error:
error = error['detail'] error = error['detail']
raise cls(f'HTTP Error {response.status_code}: {error}') raise cls(f'HTTP Error {response.status_code}: {error}')
......
...@@ -19,7 +19,7 @@ def __init__(self, api: str, resource_cls: Type[T]) -> None: ...@@ -19,7 +19,7 @@ def __init__(self, api: str, resource_cls: Type[T]) -> None:
"""The `ResourceCollection` constructor.""" """The `ResourceCollection` constructor."""
self.session = BasedSession(api) self.session = BasedSession(api)
self.deserialize: Callable[[dict], T] = resource_cls.deserialize self.deserialize: Callable[[dict], T] = resource_cls.deserialize
self.resource_type = type(resource_cls).__name__ self.resource_type = resource_cls.__name__.lower()
def __iter__(self) -> Iterator[T]: def __iter__(self) -> Iterator[T]:
"""Iterates through the resources in the collection.""" """Iterates through the resources in the collection."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment