diff --git a/src/firebolt/client/auth/request_auth_base.py b/src/firebolt/client/auth/request_auth_base.py index 6e013badb51..434ac831b0d 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,8 @@ 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 isinstance(e, HTTPStatusError): + client_friendly_error = get_auth_error(e) + error_text = client_friendly_error or 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"}