Skip to content

Commit

Permalink
feat: Add SlurmOpsError to slurm_ops
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jul 10, 2024
1 parent aeb2418 commit 9cebcd3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/charms/hpc_libs/v0/slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def _on_install(self, _) -> None:
_kebabize = re.compile(r"(?<=[a-z0-9])(?=[A-Z])")


class SlurmOpsError(Exception):
"""Exception raised when a slurm operation failed."""

@property
def message(self) -> str:
"""Return message passed as argument to exception."""
return self.args[0]


def format_key(key: str) -> str:
"""Format Slurm configuration keys from SlurmCASe into kebab case.
Expand Down Expand Up @@ -129,7 +138,7 @@ def _call(cmd: str, *args: str, stdin: Optional[str] = None) -> str:
"""Call a command with logging.
Raises:
subprocess.CalledProcessError: Raised if the command fails.
SlurmOpsError: Raised if the command fails.
"""
cmd = [cmd, *args]
_logger.debug(f"Executing command {cmd}")
Expand All @@ -138,7 +147,7 @@ def _call(cmd: str, *args: str, stdin: Optional[str] = None) -> str:
except subprocess.CalledProcessError as e:
_logger.error(f"`{' '.join(cmd)}` failed")
_logger.error(f"stderr: {e.stderr.decode()}")
raise
raise SlurmOpsError(f"command {cmd[0]} failed. Reason:\n{e.stderr.decode()}")


def _snap(*args) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_version(self, subcmd) -> None:
def test_call_error(self, subcmd) -> None:
"""Test that `slurm_ops` propagates errors when a command fails."""
subcmd.side_effect = subprocess.CalledProcessError(-1, cmd=[""], stderr=b"error")
with self.assertRaises(subprocess.CalledProcessError):
with self.assertRaises(slurm.SlurmOpsError):
slurm.install()


Expand Down

0 comments on commit 9cebcd3

Please sign in to comment.