From 2301eaa840c8a34b961285c436d63172431b4d45 Mon Sep 17 00:00:00 2001 From: ptiurin Date: Tue, 10 Oct 2023 11:36:36 +0100 Subject: [PATCH 1/2] feat: Improve auth error --- src/firebolt/client/auth/request_auth_base.py | 17 +++++++++++++++-- tests/unit/client/auth/test_request_auth.py | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/firebolt/client/auth/request_auth_base.py b/src/firebolt/client/auth/request_auth_base.py index 6e013badb51..9d1fc7f1255 100644 --- a/src/firebolt/client/auth/request_auth_base.py +++ b/src/firebolt/client/auth/request_auth_base.py @@ -1,7 +1,7 @@ from time import time from typing import Generator -from httpx import Request, Response +from httpx import HTTPStatusError, Request, Response from firebolt.client.auth.base import Auth from firebolt.client.constants import _REQUEST_ERRORS @@ -9,6 +9,13 @@ from firebolt.utils.usage_tracker import get_user_agent_header +def get_auth_error(e: HTTPStatusError) -> str: + error_description = "" + if e.response.headers.get("content-type", "").lower() == "application/json": + error_description = e.response.json().get("error_description") + return error_description + + class _RequestBasedAuth(Auth): """Base abstract class for http request based authentication.""" @@ -54,4 +61,10 @@ def get_new_token_generator(self) -> Generator[Request, Response, None]: self._expires = int(time()) + int(parsed["expires_in"]) except _REQUEST_ERRORS as e: - raise AuthenticationError(repr(e)) from e + error_text = repr(e) + if type(e) == HTTPStatusError: + client_friendly_error = get_auth_error(e) + error_text = ( + client_friendly_error if client_friendly_error else error_text + ) + raise AuthenticationError(error_text) from e diff --git a/tests/unit/client/auth/test_request_auth.py b/tests/unit/client/auth/test_request_auth.py index d0e4ef87e54..8dc80aad255 100644 --- a/tests/unit/client/auth/test_request_auth.py +++ b/tests/unit/client/auth/test_request_auth.py @@ -73,6 +73,24 @@ def http_error(*args, **kwargs): assert "Bad Request" in errmsg, "Invalid authentication error message" httpx_mock.reset(True) + # Username/password error + httpx_mock.add_response( + status_code=codes.FORBIDDEN, + headers={"Content-Type": "application/json"}, + json={ + "error": "invalid_grant", + "error_description": "Wrong email or password.", + }, + ) + with pytest.raises(AuthenticationError) as excinfo: + execute_generator_requests(auth.get_new_token_generator(), api_endpoint) + + errmsg = str(excinfo.value) + assert ( + "Wrong email or password." in errmsg + ), "Invalid authentication error message" + httpx_mock.reset(True) + # Firebolt api error httpx_mock.add_response( status_code=codes.OK, json={"error": "", "message": "firebolt"} From 05a6e2a3f48d58a668eebe0f91474d5cc91e9f9c Mon Sep 17 00:00:00 2001 From: ptiurin Date: Wed, 11 Oct 2023 17:11:05 +0100 Subject: [PATCH 2/2] better code style --- src/firebolt/client/auth/request_auth_base.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/firebolt/client/auth/request_auth_base.py b/src/firebolt/client/auth/request_auth_base.py index 9d1fc7f1255..434ac831b0d 100644 --- a/src/firebolt/client/auth/request_auth_base.py +++ b/src/firebolt/client/auth/request_auth_base.py @@ -62,9 +62,7 @@ def get_new_token_generator(self) -> Generator[Request, Response, None]: except _REQUEST_ERRORS as e: error_text = repr(e) - if type(e) == HTTPStatusError: + if isinstance(e, HTTPStatusError): client_friendly_error = get_auth_error(e) - error_text = ( - client_friendly_error if client_friendly_error else error_text - ) + error_text = client_friendly_error or error_text raise AuthenticationError(error_text) from e