Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
fix: Handle non-json response (#133)
Browse files Browse the repository at this point in the history
* Handle non-json response

* Export SeamAPIException

* Use .get to get error meta
  • Loading branch information
razor-x authored Dec 6, 2023
1 parent 93aa9fa commit 43a2450
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions seamapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# type: ignore

from seamapi.seam import Seam
from seamapi.seam import SeamAPIException
13 changes: 5 additions & 8 deletions seamapi/seam.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,10 @@ def make_request(self, method: str, path: str, **kwargs):
)


parsed_response = response.json()

if response.status_code != 200:
raise SeamAPIException(
response.status_code,
response.headers["seam-request-id"],
parsed_response["error"],
)
raise SeamAPIException(response)

if "application/json" in response.headers["content-type"]:
return response.json()

return parsed_response
return response.text
16 changes: 9 additions & 7 deletions seamapi/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
class SeamAPIException(Exception):
def __init__(
self,
status_code: int,
request_id: str,
metadata: Optional[Dict[str, any]],
response,
):
self.status_code = status_code
self.request_id = request_id
self.metadata = metadata
self.status_code = response.status_code
self.request_id = response.headers.get("seam-request-id", None)

self.metadata = None
if "application/json" in response.headers["content-type"]:
parsed_response = response.json()
self.metadata = parsed_response.get("error", None)

super().__init__(
f"SeamAPIException: status={status_code}, request_id={request_id}, metadata={metadata}"
f"SeamAPIException: status={self.status_code}, request_id={self.request_id}, metadata={self.metadata}"
)


Expand Down

0 comments on commit 43a2450

Please sign in to comment.