Skip to content

Commit

Permalink
Improve how HTTP errors are reported as client errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed May 18, 2023
1 parent 9a1c607 commit 5443207
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions mergin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,17 @@ def _do_request(self, request):
except urllib.error.HTTPError as e:
if e.headers.get("Content-Type", "") == "application/problem+json":
info = json.load(e)
raise ClientError(info.get("detail"))
raise ClientError(e.read().decode("utf-8"))
err_detail = info.get("detail")
else:
err_detail = e.read().decode("utf-8")

error_msg = (
f"HTTP Error: {e.code} {e.reason}\n"
f"URL: {request.get_full_url()}\n"
f"Method: {request.get_method()}\n"
f"Detail: {err_detail}"
)
raise ClientError(error_msg)
except urllib.error.URLError as e:
# e.g. when DNS resolution fails (no internet connection?)
raise ClientError("Error requesting " + request.full_url + ": " + str(e))
Expand Down
2 changes: 1 addition & 1 deletion mergin/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def test_available_storage_validation(mc):
mc.push_project(project_dir)
except ClientError as e:
# Expecting "You have reached a data limit" 400 server error msg.
assert str(e) == "You have reached a data limit"
assert "You have reached a data limit" in str(e)
got_right_err = True
assert got_right_err

Expand Down

0 comments on commit 5443207

Please # to comment.