From 4aab0de12ced9a55594675ad0002fe08c70abbda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20L=C3=A9obal?= Date: Wed, 2 Aug 2023 10:34:41 +0200 Subject: [PATCH] chore: look for "detail" instead of "message" in API responses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Olivier LĂ©obal --- substra/sdk/exceptions.py | 8 ++++---- tests/sdk/test_rest_client.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/substra/sdk/exceptions.py b/substra/sdk/exceptions.py index b247767b..60b113e7 100644 --- a/substra/sdk/exceptions.py +++ b/substra/sdk/exceptions.py @@ -16,7 +16,7 @@ def __init__(self, msg, status_code): def from_request_exception(cls, request_exception): msg = None try: - msg = request_exception.response.json()["message"] + msg = request_exception.response.json()["detail"] msg = f"{request_exception}: {msg}" except Exception: msg = str(request_exception) @@ -63,7 +63,7 @@ def from_request_exception(cls, request_exception): get_method = getattr(error, "get", None) if callable(get_method): - msg = get_method("message", str(error)) + msg = get_method("detail", str(error)) else: msg = str(error) @@ -91,7 +91,7 @@ def from_request_exception(cls, request_exception): r = request_exception.response.json() try: - key = r["key"] if "key" in r else r["message"].get("key") + key = r["key"] if "key" in r else r["detail"].get("key") except (AttributeError, KeyError): # XXX this is the case when doing a POST query to update the # data manager for instance @@ -197,7 +197,7 @@ class KeyAlreadyExistsError(Exception): class _TaskAssetError(Exception): - """Base eception class for task asset error""" + """Base exception class for task asset error""" def __init__(self, *, compute_task_key: str, identifier: str, message: str): self.compute_task_key = compute_task_key diff --git a/tests/sdk/test_rest_client.py b/tests/sdk/test_rest_client.py index fc758254..02a9ad45 100644 --- a/tests/sdk/test_rest_client.py +++ b/tests/sdk/test_rest_client.py @@ -48,10 +48,10 @@ def test_post_success(mocker, config): @pytest.mark.parametrize( "status_code, http_response, sdk_exception", [ - (400, {"message": "Invalid Request"}, exceptions.InvalidRequest), - (401, {"message": "Invalid username/password"}, exceptions.AuthenticationError), - (403, {"message": "Unauthorized"}, exceptions.AuthorizationError), - (404, {"message": "Not Found"}, exceptions.NotFound), + (400, {"detail": "Invalid Request"}, exceptions.InvalidRequest), + (401, {"detail": "Invalid username/password"}, exceptions.AuthenticationError), + (403, {"detail": "Unauthorized"}, exceptions.AuthorizationError), + (404, {"detail": "Not Found"}, exceptions.NotFound), (408, {"key": "a-key"}, exceptions.RequestTimeout), (408, {}, exceptions.RequestTimeout), (500, "CRASH", exceptions.InternalServerError),