From 6958c9bb1a1019f2389c2a16a8a1a48c1002de36 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 19 May 2026 10:18:16 +0200 Subject: [PATCH] show: relay cli-pretty's user-facing error messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cli-pretty prints a friendly message on stdout before any sys.exit(1) — 'Interface "w" not found', 'Error, top level "ietf-routing:routing" missing', etc. The wrapper used subprocess.run(..., check=True) and on non-zero exit caught CalledProcessError, throwing away the captured stdout and printing the generic exception message instead. Drop the check=True / try-except dance, always relay stdout, and only fall back to the generic 'Error running cli-pretty' line when the subprocess crashed without producing any output. Before: admin@bpi:/> show interface w Error running cli-pretty: Command '['/usr/libexec/statd/cli-pretty', 'show-interfaces', '-n', 'w']' returned non-zero exit status 1. After: admin@bpi:/> show interface w Interface "w" not found Signed-off-by: Joachim Wiberg --- src/bin/show/__init__.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/bin/show/__init__.py b/src/bin/show/__init__.py index 266e642b..68b2ef41 100755 --- a/src/bin/show/__init__.py +++ b/src/bin/show/__init__.py @@ -40,15 +40,20 @@ def cli_pretty(json_data: dict, command: str, *args: str): return safe_args = [shlex.quote(arg) for arg in args] + json_input = json.dumps(json_data) + result = subprocess.run([ + "/usr/libexec/statd/cli-pretty", command, *safe_args + ], input=json_input, capture_output=True, text=True) - try: - json_input = json.dumps(json_data) # Keep as string, not bytes - result = subprocess.run([ - "/usr/libexec/statd/cli-pretty", command, *safe_args - ], input=json_input, capture_output=True, text=True, check=True) + # cli-pretty prints a user-facing message on stdout before any + # sys.exit(1) (e.g. 'Interface "w" not found'). Relay it regardless + # of the exit status, and only surface the generic exec error when + # nothing useful was produced. + if result.stdout: print(result.stdout, end="") - except subprocess.CalledProcessError as e: - print(f"Error running cli-pretty: {e}") + elif result.returncode != 0: + msg = result.stderr.strip() or f"exit status {result.returncode}" + print(f"Error running cli-pretty: {msg}") def dhcp(args: List[str]) -> None: