Skip to content

Commit

Permalink
Return full error message from xmllint (#586)
Browse files Browse the repository at this point in the history
Update call_command function to include stderr output in CCPPError message.

Right now, when xmllint-ing is done via xml_tools.py (validate_xml_file),
the output of the linter is not returned. This PR adds the stderr output
(if any) to the returned error message.

PR also decodes error messages for cleaner output.

User interface changes?: No

Testing:
test removed: N/A
unit tests: Added new doctest to test error message
system tests: N/A
manual testing: Ran with/parsed new error messages within CAM-SIMA
  • Loading branch information
peverwhee authored Oct 3, 2024
1 parent af90b94 commit 49a3c3f
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions scripts/parse_tools/xml_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ def call_command(commands, logger, silent=False):
False
>>> call_command(['ls'], _LOGGER)
True
>>> try:
... call_command(['ls','--invalid-option'], _LOGGER)
... except CCPPError as e:
... print(str(e))
Execution of 'ls --invalid-option' failed with code: 2
Error output: ls: unrecognized option '--invalid-option'
Try 'ls --help' for more information.
>>> try:
... os.chdir(os.path.dirname(__file__))
... call_command(['ls', os.path.basename(__file__), 'foo.bar.baz'], _LOGGER)
... except CCPPError as e:
... print(str(e))
Execution of 'ls xml_tools.py foo.bar.baz' failed with code: 2
xml_tools.py
Error output: ls: cannot access 'foo.bar.baz': No such file or directory
"""
result = False
outstr = ''
Expand All @@ -66,9 +81,17 @@ def call_command(commands, logger, silent=False):
result = False
else:
cmd = ' '.join(commands)
emsg = "Execution of '{}' failed with code:\n"
outstr = emsg.format(cmd, err.returncode)
outstr += "{}".format(err.output)
outstr = f"Execution of '{cmd}' failed with code: {err.returncode}\n"
outstr += f"{err.output.decode('utf-8', errors='replace').strip()}"
if hasattr(err, 'stderr') and err.stderr:
stderr_str = err.stderr.decode('utf-8', errors='replace').strip()
if stderr_str:
if err.output:
outstr += os.linesep
# end if
outstr += f"Error output: {stderr_str}"
# end if
# end if
raise CCPPError(outstr) from err
# end if
# end of try
Expand Down

0 comments on commit 49a3c3f

Please sign in to comment.