Skip to content

Commit 562ebc3

Browse files
authored
feat: Improve auth error (#318)
1 parent 81cb35d commit 562ebc3

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/firebolt/client/auth/request_auth_base.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
from time import time
22
from typing import Generator
33

4-
from httpx import Request, Response
4+
from httpx import HTTPStatusError, Request, Response
55

66
from firebolt.client.auth.base import Auth
77
from firebolt.client.constants import _REQUEST_ERRORS
88
from firebolt.utils.exception import AuthenticationError
99
from firebolt.utils.usage_tracker import get_user_agent_header
1010

1111

12+
def get_auth_error(e: HTTPStatusError) -> str:
13+
error_description = ""
14+
if e.response.headers.get("content-type", "").lower() == "application/json":
15+
error_description = e.response.json().get("error_description")
16+
return error_description
17+
18+
1219
class _RequestBasedAuth(Auth):
1320
"""Base abstract class for http request based authentication."""
1421

@@ -54,4 +61,8 @@ def get_new_token_generator(self) -> Generator[Request, Response, None]:
5461
self._expires = int(time()) + int(parsed["expires_in"])
5562

5663
except _REQUEST_ERRORS as e:
57-
raise AuthenticationError(repr(e)) from e
64+
error_text = repr(e)
65+
if isinstance(e, HTTPStatusError):
66+
client_friendly_error = get_auth_error(e)
67+
error_text = client_friendly_error or error_text
68+
raise AuthenticationError(error_text) from e

tests/unit/client/auth/test_request_auth.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ def http_error(*args, **kwargs):
7373
assert "Bad Request" in errmsg, "Invalid authentication error message"
7474
httpx_mock.reset(True)
7575

76+
# Username/password error
77+
httpx_mock.add_response(
78+
status_code=codes.FORBIDDEN,
79+
headers={"Content-Type": "application/json"},
80+
json={
81+
"error": "invalid_grant",
82+
"error_description": "Wrong email or password.",
83+
},
84+
)
85+
with pytest.raises(AuthenticationError) as excinfo:
86+
execute_generator_requests(auth.get_new_token_generator(), api_endpoint)
87+
88+
errmsg = str(excinfo.value)
89+
assert (
90+
"Wrong email or password." in errmsg
91+
), "Invalid authentication error message"
92+
httpx_mock.reset(True)
93+
7694
# Firebolt api error
7795
httpx_mock.add_response(
7896
status_code=codes.OK, json={"error": "", "message": "firebolt"}

0 commit comments

Comments
 (0)