fab icon indicating copy to clipboard operation
fab copied to clipboard

Replace RuntimeError exceptions

Open t00sa opened this issue 6 months ago • 1 comments

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.

t00sa avatar Jun 11 '25 09:06 t00sa

See also #224

t00sa avatar Jul 29 '25 13:07 t00sa