Skip to content

Commit

Permalink
fix: support RequestErrorResponse a little more dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
tazlin committed Feb 17, 2024
1 parent 381de8a commit 6cfbbbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions horde_sdk/generic_api/apimodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ class RequestErrorResponse(HordeResponseBaseModel, ContainsMessageResponseMixin)
object_data: object = None
"""This is a catch all for any additional data that may be returned by the API relevant to the error."""

rc: str = "RC_MISSING"

@override
@classmethod
def get_api_model_name(cls) -> str | None:
Expand Down
12 changes: 9 additions & 3 deletions horde_sdk/generic_api/generic_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,21 @@ def _after_request_handling(
# If requests response is a failure code, see if a `message` key exists in the response.
# If so, return a RequestErrorResponse
if returned_status_code >= 400:
if len(raw_response_json) == 1 and "message" in raw_response_json:
return RequestErrorResponse(**raw_response_json)

if "errors" in raw_response_json:
raise AIHordePayloadValidationError(
raw_response_json.get("errors", ""),
raw_response_json.get("message", ""),
)

try:
return RequestErrorResponse(**raw_response_json)
except ValidationError:
return RequestErrorResponse(
message="The API returned an error we didn't recognize! See `object_data` for the raw response.",
rc=raw_response_json.get("rc", returned_status_code),
object_data={"raw_response": raw_response_json},
)

handled_response: HordeResponseTypeVar | RequestErrorResponse | None = None
try:
parsed_response = expected_response_type.model_validate(raw_response_json)
Expand Down

0 comments on commit 6cfbbbe

Please sign in to comment.