Skip to content

Commit

Permalink
chore: PassageError now correctly parses ApiException's deserialized …
Browse files Browse the repository at this point in the history
…data and raw body (#116)
  • Loading branch information
ctran88 authored Dec 11, 2024
1 parent 5f5aa54 commit 5153486
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
11 changes: 8 additions & 3 deletions passageidentity/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ def __init__(
@classmethod
def from_response_error(cls, response_error: ApiException, message: str | None = None) -> PassageError:
"""Initialize the error with a response body and optional message."""
error_code = response_error.body["code"] if response_error.body else None
error_msg = response_error.body["error"] if response_error.body else None
msg = ": ".join(filter(None, [message, error_msg]))
if response_error.data is not None:
data_dict = response_error.data.to_dict()
error_code = data_dict.get("code")
error_msg = data_dict.get("error")
msg = ": ".join(filter(None, [message, error_msg]))
else:
error_code = None
msg = str(response_error.body)

return cls(message=msg, status_code=response_error.status, error_code=error_code)
29 changes: 24 additions & 5 deletions tests/errors_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from __future__ import annotations

from passageidentity import PassageError
from passageidentity.openapi_client.exceptions import ApiException
from passageidentity.openapi_client.models.model400_error import Model400Error


class MockApiException(ApiException):
def __init__(self, status: int, body: dict) -> None:
def __init__(self, status: int, data: Model400Error | None, body: str | None = None) -> None:
self.status = status
self.data = data
self.body = body


Expand All @@ -29,26 +33,41 @@ def test_error_with_only_message() -> None:
def test_from_response_error() -> None:
response_error = MockApiException(
status=400,
body={"error": "some error", "code": "some_error_code"},
data=Model400Error(code="invalid_request", error="some error"),
)

error = PassageError.from_response_error(response_error, "some message")
assert error.message == "some message: some error"
assert error.status_code == 400
assert error.error_code == "some_error_code"
assert error.error_code == "invalid_request"
assert error.status_text is None
assert error.error is None


def test_from_response_error_without_message() -> None:
response_error = MockApiException(
status=400,
body={"error": "some error", "code": "some_error_code"},
data=Model400Error(code="invalid_request", error="some error"),
)

error = PassageError.from_response_error(response_error)
assert error.message == "some error"
assert error.status_code == 400
assert error.error_code == "some_error_code"
assert error.error_code == "invalid_request"
assert error.status_text is None
assert error.error is None


def test_from_response_error_without_data() -> None:
response_error = MockApiException(
status=400,
data=None,
body="some error",
)

error = PassageError.from_response_error(response_error)
assert error.message == "some error"
assert error.status_code == 400
assert error.error_code is None
assert error.status_text is None
assert error.error is None

0 comments on commit 5153486

Please sign in to comment.