Skip to content

Commit

Permalink
Ensure a DiceExceptionError is raised when Dice fails
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-johnson committed Sep 24, 2024
1 parent 9327089 commit 0bbf8b2
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions qiskit_addon_dice_solver/dice_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0bbf8b2

Please sign in to comment.