Skip to content

Commit

Permalink
Merge pull request #270 from artichoke/lopopolo/dry-up-error-reporting
Browse files Browse the repository at this point in the history
Factor out subprocess exception handling into a shared error reporter
  • Loading branch information
lopopolo authored Sep 17, 2024
2 parents 78193c3 + 6f37d7e commit c6145bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
38 changes: 38 additions & 0 deletions artichoke_nightly/error_reporting.py
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)
17 changes: 2 additions & 15 deletions artichoke_nightly/gpg_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dataclasses import dataclass
from pathlib import Path

from .error_reporting import report_subprocess_error
from .github_actions import emit_metadata, log_group, set_output
from .shell_utils import run_command_with_merged_output

Expand Down Expand Up @@ -123,21 +124,7 @@ def main() -> int:

set_output(name="signature", value=str(signature))
except subprocess.CalledProcessError as e:
print("Error: failed to invoke command", file=sys.stderr)
print(f" Command: {e.cmd}", file=sys.stderr)
print(f" Return Code: {e.returncode}", file=sys.stderr)
if e.stdout:
print()
print("Output:", file=sys.stderr)
for line in e.stdout.splitlines():
print(f" {line}", file=sys.stderr)
if e.stderr:
print()
print("Error Output:", file=sys.stderr)
for line in e.stderr.splitlines():
print(f" {line}", file=sys.stderr)
print()
print(traceback.format_exc(), file=sys.stderr)
report_subprocess_error(e, output=sys.stderr)
return e.returncode
except Exception as e: # noqa: BLE001
print(f"Error: {e}", file=sys.stderr)
Expand Down
17 changes: 2 additions & 15 deletions artichoke_nightly/macos_sign_and_notarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import stamina
import validators

from .error_reporting import report_subprocess_error
from .github_actions import emit_metadata, log_group, runner_tempdir, set_output
from .shell_utils import run_command_with_merged_output

Expand Down Expand Up @@ -915,21 +916,7 @@ def main() -> int:
set_output(name="asset", value=str(bundle))
set_output(name="content_type", value="application/x-apple-diskimage")
except subprocess.CalledProcessError as e:
print("Error: failed to invoke command", file=sys.stderr)
print(f" Command: {e.cmd}", file=sys.stderr)
print(f" Return Code: {e.returncode}", file=sys.stderr)
if e.stdout:
print()
print("Output:", file=sys.stderr)
for line in e.stdout.splitlines():
print(f" {line}", file=sys.stderr)
if e.stderr:
print()
print("Error Output:", file=sys.stderr)
for line in e.stderr.splitlines():
print(f" {line}", file=sys.stderr)
print()
print(traceback.format_exc(), file=sys.stderr)
report_subprocess_error(e, output=sys.stderr)
return e.returncode
except Exception as e: # noqa: BLE001
print(f"Error: {e}", file=sys.stderr)
Expand Down

0 comments on commit c6145bc

Please sign in to comment.