-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from artichoke/lopopolo/dry-up-error-reporting
Factor out subprocess exception handling into a shared error reporter
- Loading branch information
Showing
3 changed files
with
42 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import subprocess | ||
import textwrap | ||
import traceback | ||
from typing import TextIO | ||
|
||
|
||
def report_subprocess_error( | ||
e: subprocess.CalledProcessError, *, output: TextIO | ||
) -> None: | ||
""" | ||
Prints details of a subprocess error to the specified output stream, | ||
suitable for use in CLI entry points. | ||
Args: | ||
e (subprocess.CalledProcessError): The exception containing details | ||
about the failed subprocess command, including command, return | ||
code, stdout, and stderr. | ||
output (TextIO): The output stream (e.g., file or stderr) where error | ||
details will be printed. | ||
Prints the command, return code, stdout, stderr (if available), and the | ||
traceback to the given output stream, making it ideal for error reporting | ||
in command-line applications. | ||
""" | ||
|
||
print("Error: failed to invoke command", file=output) | ||
print(f" Command: {e.cmd}", file=output) | ||
print(f" Return Code: {e.returncode}", file=output) | ||
|
||
if e.stdout and e.stdout.rstrip(): | ||
print("", "Output:", sep="\n", file=output) | ||
print(textwrap.indent(e.stdout.rstrip(), " "), file=output) | ||
|
||
if e.stderr and e.stderr.rstrip(): | ||
print("", "Error Output:", sep="\n", file=output) | ||
print(textwrap.indent(e.stderr.rstrip(), " "), file=output) | ||
|
||
print("", traceback.format_exc(), sep="\n", file=output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters