Skip to content

Commit

Permalink
[sonic_installer]: Improve exception handling: introduce notes.
Browse files Browse the repository at this point in the history
Signed-off-by: Nazarii Hnydyn <nazariig@nvidia.com>
  • Loading branch information
nazariig committed Oct 30, 2023
1 parent 0ae5d2d commit 1b5c7c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 7 additions & 2 deletions sonic_installer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ def run_command_or_raise(argv, raise_exception=True, capture=True):

stdout = subprocess.PIPE if capture else None
proc = subprocess.Popen(argv, text=True, stdout=stdout)
out, _ = proc.communicate()
out, err = proc.communicate()

if proc.returncode != 0 and raise_exception:
raise SonicRuntimeException("Failed to run command '{0}'".format(argv))
sre = SonicRuntimeException("Failed to run command '{0}'".format(argv))
if out:
sre.add_note("\nSTDOUT:\n{}".format(out.rstrip("\n")))
if err:
sre.add_note("\nSTDERR:\n{}".format(err.rstrip("\n")))
raise sre

if out is not None:
out = out.rstrip("\n")
Expand Down
13 changes: 12 additions & 1 deletion sonic_installer/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,15 @@
class SonicRuntimeException(Exception):
"""SONiC Runtime Excpetion class used to report SONiC related errors
"""
pass
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.notes = []

def __str__(self):
msg = super().__str__()
if self.notes:
msg += "\n" + "\n".join(self.notes)
return msg

def add_note(self, note):
self.notes.append(note)

0 comments on commit 1b5c7c5

Please sign in to comment.