[PATCH 2/4] u_boot_pylib: Add an exception-class for errors
Simon Glass
sjg at chromium.org
Mon Feb 3 17:26:43 CET 2025
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 at chromium.org>
---
tools/patman/gitutil.py | 2 +-
tools/u_boot_pylib/command.py | 20 ++++++++++++++++++--
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/tools/patman/gitutil.py b/tools/patman/gitutil.py
index 10ea5ff39f5..ffe05273b35 100644
--- a/tools/patman/gitutil.py
+++ b/tools/patman/gitutil.py
@@ -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
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):
--
2.43.0
More information about the U-Boot
mailing list