Skip to content

Improve how HTTP errors are reported as client errors #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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