From 0bbf8b2d741659ce9fb3fd1dc141c09295f220e1 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Tue, 24 Sep 2024 15:54:49 -0500 Subject: [PATCH] Ensure a DiceExceptionError is raised when Dice fails --- qiskit_addon_dice_solver/dice_solver.py | 29 +++++++++++-------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/qiskit_addon_dice_solver/dice_solver.py b/qiskit_addon_dice_solver/dice_solver.py index 77f5b1d..540793e 100644 --- a/qiskit_addon_dice_solver/dice_solver.py +++ b/qiskit_addon_dice_solver/dice_solver.py @@ -33,18 +33,15 @@ class DiceExecutionError(Exception): """Custom exception for Dice command line application execution errors.""" - def __init__(self, command, returncode, output, stderr, working_dir): + def __init__(self, command, returncode, log_path): """Initialize a ``DiceExecutionError`` instance.""" self.command = command self.returncode = returncode - self.output = output - self.stderr = stderr - self.working_dir = working_dir + self.log_path = log_path + message = ( f"Command '{command}' failed with return code {returncode}\n" - f"Working directory: {working_dir}\n" - f"Output: {output}\n" - f"Error: {stderr}" + f"See the log file at {log_path} for more details." ) super().__init__(message) @@ -193,17 +190,17 @@ def _call_dice(working_dir: Path, mpirun_options: Sequence[str] | str | None) -> else: dice_call = ["mpirun", dice_path] - try: - with open(dice_log_path, "w") as logfile: - subprocess.run(dice_call, cwd=working_dir, stdout=logfile, stderr=logfile) - except subprocess.CalledProcessError as e: + with open(dice_log_path, "w") as logfile: + process = subprocess.run( + dice_call, cwd=working_dir, stdout=logfile, stderr=logfile + ) + + if process.returncode != 0: raise DiceExecutionError( command=dice_call, - returncode=e.returncode, - output=e.stdout, - stderr=e.stderr, - working_dir=working_dir, - ) from e + returncode=process.returncode, + log_path=dice_log_path, + ) def _write_input_files(