Skip to content

Commit c906aa9

Browse files
authored
fix(FIR-42216): improve authentication error message (#409)
1 parent 8b5a560 commit c906aa9

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/firebolt/client/auth/request_auth_base.py

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

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

66
from firebolt.client.auth.base import Auth
77
from firebolt.client.constants import _REQUEST_ERRORS
8-
from firebolt.utils.exception import AuthenticationError
8+
from firebolt.utils.exception import AuthenticationError, AuthorizationError
99
from firebolt.utils.usage_tracker import get_user_agent_header
1010

1111

@@ -54,4 +54,6 @@ def get_new_token_generator(self) -> Generator[Request, Response, None]:
5454
self._expires = int(time()) + int(parsed["expires_in"])
5555

5656
except _REQUEST_ERRORS as e:
57+
if e.response.status_code == codes.UNAUTHORIZED:
58+
raise AuthorizationError()
5759
raise AuthenticationError(repr(e)) from e

src/firebolt/utils/exception.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict
1+
from typing import Any, Dict, Optional
22

33

44
class FireboltError(Exception):
@@ -179,17 +179,22 @@ def __init__(self, cause: str):
179179

180180

181181
class AuthorizationError(FireboltError):
182-
"""Firebolt authentication error.
182+
"""Firebolt authorisation error.
183183
184184
Args:
185185
cause (str): Reason for authorization failure
186186
187-
Attributes:
188-
cause (str): Reason for authorization failure
189187
"""
190188

191-
def __init__(self, cause: str):
192-
super().__init__(f"Authorization failed: {cause}.")
189+
_default_error_message = (
190+
"Failed to connect to Firebolt. Could not authenticate with the given "
191+
"credentials. Please verify the provided credentials are up to date and "
192+
"correct and that you have the correct user permissions"
193+
)
194+
195+
def __init__(self, cause: Optional[str] = None):
196+
error_cause = cause if cause else self._default_error_message
197+
super().__init__(f"Authorization failed: {error_cause}.")
193198

194199

195200
# PEP-249

tests/unit/client/auth/test_request_auth.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pytest_mock import MockerFixture
77

88
from firebolt.client.auth import ClientCredentials
9-
from firebolt.utils.exception import AuthenticationError
9+
from firebolt.utils.exception import AuthenticationError, AuthorizationError
1010
from tests.unit.util import execute_generator_requests
1111

1212

@@ -44,6 +44,16 @@ def http_error(*args, **kwargs):
4444
assert str(excinfo.value) == "httpx", "Invalid authentication error message"
4545
httpx_mock.reset(True)
4646

47+
# HTTP error
48+
httpx_mock.add_response(status_code=codes.UNAUTHORIZED)
49+
with pytest.raises(AuthorizationError) as excinfo:
50+
execute_generator_requests(auth.get_new_token_generator(), api_endpoint)
51+
errmsg = str(excinfo.value)
52+
assert (
53+
"Please verify the provided credentials" in errmsg
54+
), "Invalid authorization error message"
55+
httpx_mock.reset(True)
56+
4757
# HTTP error
4858
httpx_mock.add_response(status_code=codes.BAD_REQUEST)
4959
with pytest.raises(AuthenticationError) as excinfo:

0 commit comments

Comments
 (0)