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

refactor(connection): call specific error response codes (DEV-3339) #832

Merged
merged 12 commits into from
Feb 29, 2024
Merged
Changes from 9 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
33 changes: 20 additions & 13 deletions src/dsp_tools/utils/connection_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import regex
from requests import ReadTimeout, RequestException, Response, Session

from dsp_tools.models.exceptions import BadCredentialsError, BaseError, PermanentConnectionError, UserError
from dsp_tools.models.exceptions import BadCredentialsError, BaseError, InputError, 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__)
LOGFILES = ", ".join([handler.baseFilename for handler in logger.handlers if isinstance(handler, FileHandler)])
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved


@dataclass
Expand Down Expand Up @@ -252,7 +253,6 @@ def _try_network_action(self, params: RequestParameters) -> Response:
Returns:
the return value of action
"""
logfiles = ", ".join([handler.baseFilename for handler in logger.handlers if isinstance(handler, FileHandler)])
action = partial(self.session.request, **params.as_kwargs())
for i in range(7):
try:
Expand All @@ -269,21 +269,28 @@ 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 "OntologyConstraintException" in response.text:
msg = f"Permanently unable to execute the network action. See logs for more details: {logfiles}"
raise PermanentConnectionError(msg)
elif not self._in_testing_environment():
self._log_and_sleep(reason="Non-200 response code", retry_counter=i, exc_info=False)
continue
else:
msg = f"Permanently unable to execute the network action. See logs for more details: {logfiles}"
raise PermanentConnectionError(msg)

self._handle_non_ok_responses(response, params.url, i)

# after 7 vain attempts to create a response, try it a last time and let it escalate
return action()

def _handle_non_ok_responses(self, response: Response, request_url: str, retry_counter: int) -> None:
if "v2/authentication" in request_url and response.status_code == HTTP_UNAUTHORIZED:
raise BadCredentialsError("Bad credentials")

elif any(x in response.text for x in ["OntologyConstraintException", "DuplicateValueException"]):
msg = f"Error occurred due to user input, original message:\n{response.text}"
raise InputError(msg)

elif not self._in_testing_environment():
self._log_and_sleep(reason="Non-200 response code", retry_counter=retry_counter, exc_info=False)
return None

else:
msg = f"Permanently unable to execute the network action. See logs for more details: {LOGFILES}"
raise PermanentConnectionError(msg)

def _renew_session(self) -> None:
self.session.close()
self.session = Session()
Expand Down