Skip to content

Commit

Permalink
Handle non-json error responses (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
willkg committed Aug 13, 2024
1 parent d4b786b commit 7e2f7f3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/crashstats_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,21 @@ def http_get(url, params, api_token=None):

# Handle 403 so we can provide the user more context
if api_token and resp.status_code == 403:
raise BadAPIToken(resp.json().get("error", "No error provided"))
try:
error = resp.json().get("error", "No error provided")
except requests.exceptions.JSONDecodeError:
error = resp.text or "no response provided"

raise BadAPIToken(f"HTTP {resp.status_code}: {error}")

# Handle 400 which indicates a problem with the request
if resp.status_code == 400:
raise BadRequest(resp.json().get("error", "No error provided"))
try:
error = resp.json().get("error", "No error provided")
except requests.exceptions.JSONDecodeError:
error = resp.text or "no response provided"

raise BadRequest(f"HTTP {resp.status_code}: {error}")

# Raise an error for any other non-200 response
resp.raise_for_status()
Expand Down

0 comments on commit 7e2f7f3

Please sign in to comment.