diff options
author | Simon Glass <sjg@chromium.org> | 2025-02-03 09:26:43 -0700 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2025-03-04 13:31:49 -0600 |
commit | 54ead4be04241f34967a6a591d529ee8ba66f301 (patch) | |
tree | 5a03136afa56df458e4c3f7a911b3dac3c3f9477 /tools/u_boot_pylib/command.py | |
parent | d6900a778a72ddb33b10550503719a13cc59bc18 (diff) |
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>
Diffstat (limited to 'tools/u_boot_pylib/command.py')
-rw-r--r-- | tools/u_boot_pylib/command.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/tools/u_boot_pylib/command.py b/tools/u_boot_pylib/command.py index 103358420dd..4a9916bd814 100644 --- a/tools/u_boot_pylib/command.py +++ b/tools/u_boot_pylib/command.py @@ -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): |