Skip to content

Replace RuntimeError exceptions #421

@t00sa

Description

@t00sa

The current code over-uses the python exception hierarchy, especially RuntimeError exceptions, where custom exception classes would be better. This makes it difficult to differentiate between legitimate errors and known or expected conditions.

For example, source/fab/tools/tool.py contains the following:

if self._is_available is False:
    raise RuntimeError(f"Tool '{self.name}' is not available to run "
                       f"'{command}'.")
try:
    res = subprocess.run(command, capture_output=capture_output,
                         env=env, cwd=cwd, check=False)
except FileNotFoundError as err:
    raise RuntimeError(f"Command '{command}' could not be "
                       f"executed.") from err
if res.returncode != 0:
    msg = (f'Command failed with return code {res.returncode}:\n'
           f'{command}')
    if res.stdout:
        msg += f'\n{res.stdout.decode()}'
    if res.stderr:
        msg += f'\n{res.stderr.decode()}'
    raise RuntimeError(msg)

Which prevents the caller from determining whether the condition was a configuration issue, whether it was a legitimate failure because the compiler being targeted was not installed, or whether an error occurred running the command.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions