Skip to content

Commit

Permalink
fix(xmlupload): don't stop xmlupload when resource is wrong (DEV-3419) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Mar 13, 2024
1 parent 9b9b50b commit 86795ec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Expand Up @@ -32,7 +32,7 @@
from dsp_tools.commands.xmlupload.upload_config import UploadConfig
from dsp_tools.commands.xmlupload.write_diagnostic_info import write_id2iri_mapping
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.exceptions import PermanentConnectionError
from dsp_tools.models.exceptions import PermanentTimeOutError
from dsp_tools.models.exceptions import UserError
from dsp_tools.models.exceptions import XmlUploadInterruptedError
from dsp_tools.models.projectContext import ProjectContext
Expand Down Expand Up @@ -427,7 +427,7 @@ def _upload_resources(
# resource creation succeeded: update the iri_resolver and remove the resource from the list
iri, label = res
_tidy_up_resource_creation(iri, label, iri_resolver, resource, current_res, total_res) # type: ignore[arg-type]
except PermanentConnectionError:
except PermanentTimeOutError:
msg = (
f"There was a timeout while trying to create resource '{resource.res_id}'.\n"
f"It is unclear if the resource '{resource.res_id}' was created successfully or not.\n"
Expand Down Expand Up @@ -474,9 +474,9 @@ def _create_resource(
) -> tuple[str, str] | tuple[None, None]:
try:
return resource_create_client.create_resource(resource, bitstream_information)
except PermanentConnectionError as err:
except PermanentTimeOutError as err:
# The following block catches all exceptions and handles them in a generic way.
# Because the calling function needs to know that this was a PermanentConnectionError, we need to catch and
# Because the calling function needs to know that this was a PermanentTimeOutError, we need to catch and
# raise it here.
raise err
except Exception as err:
Expand Down
4 changes: 4 additions & 0 deletions src/dsp_tools/models/exceptions.py
Expand Up @@ -59,6 +59,10 @@ class PermanentConnectionError(BaseError):
"""This error is raised when all attempts to reconnect to DSP have failed."""


class PermanentTimeOutError(BaseError):
"""This error is raised when python throws a timeout due to no response from the DSP-API."""


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

Expand Down
8 changes: 6 additions & 2 deletions src/dsp_tools/utils/connection_live.py
Expand Up @@ -20,6 +20,7 @@
from dsp_tools.models.exceptions import BadCredentialsError
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.exceptions import PermanentConnectionError
from dsp_tools.models.exceptions import PermanentTimeOutError
from dsp_tools.models.exceptions import UserError
from dsp_tools.utils.create_logger import get_log_filename_str
from dsp_tools.utils.create_logger import get_logger
Expand Down Expand Up @@ -294,7 +295,10 @@ def _handle_non_ok_responses(self, response: Response, request_url: str, retry_c
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}"
msg = "Permanently unable to execute the network action. "
if original_str := regex.search(r'{"knora-api:error":"dsp\.errors\.(.*)","@context', str(response.content)):
msg += f"\n{' '*37}Original Message: {original_str.group(1)}\n"
msg += f"See logs for more details: {LOGFILES}"
raise PermanentConnectionError(msg)

def _renew_session(self) -> None:
Expand All @@ -314,7 +318,7 @@ def _log_and_raise_timeouts(self, error: TimeoutError | ReadTimeout) -> None:
msg = f"A '{error.__class__.__name__}' occurred during the connection to the DSP server."
print(f"{datetime.now()}: {msg}")
logger.exception(msg)
raise PermanentConnectionError(msg) from None
raise PermanentTimeOutError(msg) from None

def _log_response(self, response: Response) -> None:
dumpobj: dict[str, Any] = {
Expand Down
3 changes: 2 additions & 1 deletion test/unittests/utils/test_connection_live.py
Expand Up @@ -15,6 +15,7 @@
from requests import RequestException

from dsp_tools.models.exceptions import PermanentConnectionError
from dsp_tools.models.exceptions import PermanentTimeOutError
from dsp_tools.models.exceptions import UserError
from dsp_tools.utils.connection_live import ConnectionLive
from dsp_tools.utils.connection_live import RequestParameters
Expand Down Expand Up @@ -268,7 +269,7 @@ def test_try_network_action_timeout_error(monkeypatch: pytest.MonkeyPatch) -> No
con._log_response = Mock()
params = RequestParameters(method="GET", url="http://example.com/", timeout=1)
expected_msg = regex.escape("A 'TimeoutError' occurred during the connection to the DSP server.")
with pytest.raises(PermanentConnectionError, match=expected_msg):
with pytest.raises(PermanentTimeOutError, match=expected_msg):
con._try_network_action(params)


Expand Down

0 comments on commit 86795ec

Please sign in to comment.