Skip to content

Commit

Permalink
Merge pull request #136 from seqeralabs/error_catching
Browse files Browse the repository at this point in the history
fix: catch errors more reliably
  • Loading branch information
ejseqera authored Apr 25, 2024
2 parents b624a73 + 382454f commit 17ed8e4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions seqerakit/seqeraplatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def _execute_command(self, full_cmd, to_json=False):
stdout, _ = process.communicate()
stdout = stdout.decode("utf-8").strip()

if "ERROR: " in stdout:
self._handle_command_errors(stdout)
if "ERROR: " in stdout or process.returncode != 0:
self._handle_command_errors(str(stdout))

return json.loads(stdout) if to_json else stdout

Expand All @@ -114,11 +114,11 @@ def _handle_command_errors(self, stdout):
r"ERROR: .*already (exists|a participant)", stdout, flags=re.IGNORECASE
):
raise ResourceExistsError(
" Resource already exists. Please delete first or set 'overwrite: true'"
"Resource already exists. Please delete first or set 'overwrite: true'"
)
else:
raise ResourceCreationError(
f" Resource creation failed: '{stdout}'. "
f"Resource creation failed: '{stdout}'. "
"Check your config and try again."
)

Expand Down
6 changes: 5 additions & 1 deletion tests/unit/test_seqeraplatform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from unittest.mock import patch
from unittest.mock import MagicMock, patch
from seqerakit import seqeraplatform
import json
import subprocess
Expand All @@ -26,6 +26,7 @@ def test_run_with_jsonout_command(self, mock_subprocess):
"dateCreated": "2023-02-15T13:14:30Z",
}
# Mock the stdout of the Popen process
mock_subprocess.return_value = MagicMock(returncode=0)
mock_subprocess.return_value.communicate.return_value = (
json.dumps(mock_pipelines_json).encode(),
b"",
Expand Down Expand Up @@ -93,6 +94,7 @@ def test_resource_creation_error(self):
def test_json_parsing(self):
with patch("subprocess.Popen") as mock_subprocess:
# Mock the stdout of the Popen process to return JSON
mock_subprocess.return_value = MagicMock(returncode=0)
mock_subprocess.return_value.communicate.return_value = (
b'{"key": "value"}',
b"",
Expand All @@ -112,6 +114,7 @@ def setUp(self):
@patch("subprocess.Popen")
def test_cli_args_inclusion(self, mock_subprocess):
# Mock the stdout of the Popen process
mock_subprocess.return_value = MagicMock(returncode=0)
mock_subprocess.return_value.communicate.return_value = (
json.dumps({"key": "value"}).encode(),
b"",
Expand All @@ -136,6 +139,7 @@ def test_cli_args_inclusion_ssl_certs(self, mock_subprocess):
seqeraplatform.SeqeraPlatform(cli_args=self.cli_args)

# Mock the stdout of the Popen process
mock_subprocess.return_value = MagicMock(returncode=0)
mock_subprocess.return_value.communicate.return_value = (
json.dumps({"key": "value"}).encode(),
b"",
Expand Down

0 comments on commit 17ed8e4

Please sign in to comment.