From 5443207f06f7539f482d88284305d7c93d301c47 Mon Sep 17 00:00:00 2001 From: Martin Dobias Date: Thu, 18 May 2023 17:15:46 +0200 Subject: [PATCH] Improve how HTTP errors are reported as client errors --- mergin/client.py | 13 +++++++++++-- mergin/test/test_client.py | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/mergin/client.py b/mergin/client.py index 8e098742..ee241a5d 100644 --- a/mergin/client.py +++ b/mergin/client.py @@ -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)) diff --git a/mergin/test/test_client.py b/mergin/test/test_client.py index f6f1c329..be3c415d 100644 --- a/mergin/test/test_client.py +++ b/mergin/test/test_client.py @@ -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