Skip to content

Commit

Permalink
feat: Add AxiomError for more detailed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bahlo committed Sep 24, 2024
1 parent 6890a1b commit 5fe9ccf
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/axiom_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,35 @@ class AplOptions:
includeCursor: bool = field(default=False)


def raise_response_error(r):
if r.status_code >= 400:
# TODO: Decode JSON https://github.com/axiomhq/axiom-go/blob/610cfbd235d3df17f96a4bb156c50385cfbd9edd/axiom/error.go#L35-L50
r.raise_for_status()
class AxiomError(Exception):
"""This exception is raised on request errors."""

status: int
message: str

@dataclass
class Response:
message: str
error: Optional[str]

def __init__(self, status: int, res: Response):
message = res.error if res.error is not None else res.message
super().__init__(f"API error {status}: {message}")

self.status = status
self.message = message


def raise_response_error(res):
if res.status_code >= 400:
try:
error_res = from_dict(AxiomError.Response, res.json())
except Exception:
# Response is not in the Axiom JSON format, create generic error
# message
error_res = AxiomError.Response(message=res.reason, error=None)

raise AxiomError(res.status_code, error_res)


class Client: # pylint: disable=R0903
Expand Down

0 comments on commit 5fe9ccf

Please sign in to comment.