Skip to content

Commit

Permalink
fix: don't retry on errors in the 400 range (DEV-3349) (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Mar 12, 2024
1 parent f1df58b commit c543274
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Expand Up @@ -3,7 +3,6 @@
import pickle
import sys
from datetime import datetime
from logging import FileHandler
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -38,6 +37,7 @@
from dsp_tools.models.projectContext import ProjectContext
from dsp_tools.utils.connection import Connection
from dsp_tools.utils.connection_live import ConnectionLive
from dsp_tools.utils.create_logger import get_log_filename_str
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.json_ld_util import get_json_ld_context_for_project

Expand Down Expand Up @@ -150,7 +150,7 @@ def cleanup_upload(
logger.info("All resources have successfully been uploaded.")
else:
success = False
logfiles = ", ".join([handler.baseFilename for handler in logger.handlers if isinstance(handler, FileHandler)])
logfiles = get_log_filename_str(logger)
if failed_uploads:
print(f"\n{datetime.now()}: WARNING: Could not upload the following resources: {failed_uploads}\n")
print(f"For more information, see the log file: {logfiles}\n")
Expand Down Expand Up @@ -508,7 +508,7 @@ def _handle_upload_error(
msg = "\n==========================================\n" + err.message + "\n"
exit_code = 0
else:
logfiles = ", ".join([handler.baseFilename for handler in logger.handlers if isinstance(handler, FileHandler)])
logfiles = get_log_filename_str(logger)
msg = (
f"\n==========================================\n"
f"{datetime.now()}: xmlupload must be aborted because of an error.\n"
Expand Down
20 changes: 5 additions & 15 deletions src/dsp_tools/utils/connection_live.py
Expand Up @@ -19,7 +19,6 @@

from dsp_tools.models.exceptions import BadCredentialsError
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.exceptions import InputError
from dsp_tools.models.exceptions import PermanentConnectionError
from dsp_tools.models.exceptions import UserError
from dsp_tools.utils.create_logger import get_log_filename_str
Expand Down Expand Up @@ -287,23 +286,14 @@ def _try_network_action(self, params: RequestParameters) -> Response:
return action()

def _handle_non_ok_responses(self, response: Response, request_url: str, retry_counter: int) -> None:
permanent_error_regexes = [
r"OntologyConstraintException",
r"DuplicateValueException",
r"Project '[0-9A-F]{4}' not found",
r"No projects found",
]
if "v2/authentication" in request_url and response.status_code == HTTP_UNAUTHORIZED:
raise BadCredentialsError("Bad credentials")

elif any(regex.search(x, response.text) for x in permanent_error_regexes):
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)
in_500_range = 500 <= response.status_code < 600
try_again_later = "try again later" in response.text.lower()
should_retry = (try_again_later or in_500_range) and not self._in_testing_environment()
if should_retry:
self._log_and_sleep("Transient Error", 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)
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/utils/test_connection_live.py
Expand Up @@ -296,7 +296,7 @@ def test_try_network_action_connection_error(monkeypatch: pytest.MonkeyPatch) ->
def test_try_network_action_non_200(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("DSP_TOOLS_TESTING", raising=False) # in CI, this variable suppresses the retrying mechanism
con = ConnectionLive("http://example.com/")
responses = (Mock(status_code=500, text=""), Mock(status_code=404, text=""), Mock(status_code=200, text=""))
responses = (Mock(status_code=500, text=""), Mock(status_code=506, text=""), Mock(status_code=200, text=""))
session_mock = SessionMock(responses)
con.session = session_mock # type: ignore[assignment]
con._log_request = Mock()
Expand Down

0 comments on commit c543274

Please sign in to comment.