Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't retry login when credentials are invalid (DEV-3224) #763

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/dsp_tools/models/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class PermanentConnectionError(BaseError):
message: str


class BadCredentialsError(PermanentConnectionError):
"""This error is raised when DSP-API doesn't accept the prodived credentials."""


class XmlUploadError(BaseError):
"""
Represents an error raised in the context of the XML import.
Expand Down
13 changes: 9 additions & 4 deletions src/dsp_tools/utils/connection_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
from requests import JSONDecodeError, ReadTimeout, RequestException, Response, Session
from urllib3.exceptions import ReadTimeoutError

from dsp_tools.models.exceptions import BaseError, PermanentConnectionError, UserError
from dsp_tools.models.exceptions import BadCredentialsError, BaseError, PermanentConnectionError, UserError
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.set_encoder import SetEncoder

HTTP_OK = 200
HTTP_UNAUTHORIZED = 401

logger = get_logger(__name__)

Expand Down Expand Up @@ -81,17 +82,18 @@ def login(self, email: str, password: str) -> None:
Raises:
UserError: if DSP-API returns no token with the provided user credentials
"""
err_msg = f"Username and/or password are not valid on server '{self.server}'"
try:
response = self.post(
route="/v2/authentication",
data={"email": email, "password": password},
timeout=10,
)
except BadCredentialsError:
raise UserError(f"Username and/or password are not valid on server '{self.server}'") from None
except PermanentConnectionError as e:
raise UserError(err_msg) from e
raise UserError(e.message) from None
if not response.get("token"):
raise UserError(err_msg)
raise UserError("Unable to retrieve a token from the server with the provided credentials.")
self.token = response["token"]
self.session.headers["Authorization"] = f"Bearer {self.token}"

Expand Down Expand Up @@ -240,6 +242,7 @@ def _try_network_action(self, params: RequestParameters) -> Response:
params: keyword arguments for the HTTP request

Raises:
BadCredentialsError: if the server returns a 401 status code on the route /v2/authentication
PermanentConnectionError: if the server returns a permanent error
unexpected exceptions: if the action fails with an unexpected exception

Expand All @@ -262,6 +265,8 @@ def _try_network_action(self, params: RequestParameters) -> Response:
self._log_response(response)
if response.status_code == HTTP_OK:
return response
elif "v2/authentication" in params.url and response.status_code == HTTP_UNAUTHORIZED:
raise BadCredentialsError("Bad credentials")
elif not self._in_testing_environment():
self._log_and_sleep(reason="Non-200 response code", retry_counter=i, exc_info=False)
continue
Expand Down