u_boot_pylib: Add an exception-class for errors

Throwing an Exception is not very friendly since it is the top-level
class of all exceptions. Declare a new class instead.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2025-02-03 09:26:43 -07:00 committed by Tom Rini
parent d6900a778a
commit 54ead4be04
2 changed files with 19 additions and 3 deletions

View file

@ -140,7 +140,7 @@ def get_upstream(git_dir, branch):
'branch.%s.remote' % branch)
merge = command.output_one_line('git', '--git-dir', git_dir, 'config',
'branch.%s.merge' % branch)
except Exception:
except command.CommandExc:
upstream, msg = guess_upstream(git_dir, branch)
return upstream, msg

View file

@ -13,6 +13,19 @@ from u_boot_pylib import cros_subprocess
# When this value is None, commands are executed as normal.
TEST_RESULT = None
class CommandExc(Exception):
"""Reports an exception to the caller"""
def __init__(self, msg, result):
"""Set up a new exception object
Args:
result (CommandResult): Execution result so far
"""
super().__init__(msg)
self.result = result
"""Shell command ease-ups for Python."""
class CommandResult:
@ -61,6 +74,8 @@ def run_pipe(pipe_list, infile=None, outfile=None,
kwargs: Additional keyword arguments to cros_subprocess.Popen()
Returns:
CommandResult object
Raises:
CommandExc if an exception happens
"""
if TEST_RESULT:
if hasattr(TEST_RESULT, '__call__'):
@ -95,7 +110,8 @@ def run_pipe(pipe_list, infile=None, outfile=None,
except Exception as err:
result.exception = err
if raise_on_error:
raise Exception("Error running '%s': %s" % (user_pipestr, str))
raise CommandExc(f"Error running '{user_pipestr}': {err}",
result) from err
result.return_code = 255
return result.to_output(binary)
@ -106,7 +122,7 @@ def run_pipe(pipe_list, infile=None, outfile=None,
result.output = result.stdout.rstrip(b'\r\n')
result.return_code = last_pipe.wait()
if raise_on_error and result.return_code:
raise Exception("Error running '%s'" % user_pipestr)
raise CommandExc(f"Error running '{user_pipestr}'", result)
return result.to_output(binary)
def output(*cmd, **kwargs):