diff --git a/lib/charms/hpc_libs/v0/slurm_ops.py b/lib/charms/hpc_libs/v0/slurm_ops.py index d72c145..03e9c89 100644 --- a/lib/charms/hpc_libs/v0/slurm_ops.py +++ b/lib/charms/hpc_libs/v0/slurm_ops.py @@ -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. @@ -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}") @@ -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: diff --git a/tests/unit/test_slurm_ops.py b/tests/unit/test_slurm_ops.py index 652f02c..45d4a6e 100644 --- a/tests/unit/test_slurm_ops.py +++ b/tests/unit/test_slurm_ops.py @@ -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()