Skip to content

Commit

Permalink
feat: add non-JSON response error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tengzl33t committed Sep 3, 2024
1 parent 8ee4f5f commit d9a7b15
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion threatx_api_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import importlib.metadata
from json import JSONDecodeError

import aiohttp

Expand Down Expand Up @@ -59,7 +60,16 @@ async def __post(self, session, path: str, post_payload: dict):
clean_post_payload.pop("marker_var", None)

async with session.post(path, json=clean_post_payload) as raw_response:
response = await raw_response.json(content_type=None)
try:
response = await raw_response.json(content_type=None)
except JSONDecodeError:
request_id = raw_response.headers.get("X-Request-ID")
raise TXAPIResponseError(
f"Could not parse the API response.\n"
f"Request ID: {request_id}\n"
f"Please contact: support@threatx.com"
)

response_ok_data = response.get("Ok")
response_error_data = response.get("Error")

Expand Down

0 comments on commit d9a7b15

Please sign in to comment.