From 1a5655507699b329db555acd0ddee60fb7fac4a4 Mon Sep 17 00:00:00 2001 From: Richard Alpe Date: Wed, 27 Sep 2023 08:55:31 +0200 Subject: [PATCH] cli: add graceful error for missing json in cli-pretty Print a graceful error message if the json data is missing of invalid. Signed-off-by: Richard Alpe --- board/netconf/rootfs/lib/infix/cli-pretty | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/board/netconf/rootfs/lib/infix/cli-pretty b/board/netconf/rootfs/lib/infix/cli-pretty index bc8fa8e7c..018508c02 100755 --- a/board/netconf/rootfs/lib/infix/cli-pretty +++ b/board/netconf/rootfs/lib/infix/cli-pretty @@ -251,11 +251,21 @@ def ietf_interfaces(json, name): sys.exit(1) return iface.pr_iface() +def main(): + try: + json_data = json.load(sys.stdin) + except json.JSONDecodeError: + print("Error, invalid JSON input") + sys.exit(1) + except Exception as e: + print("Error, unexpected error parsing JSON") + sys.exit(1) -json = json.load(sys.stdin) + if args.module == "ietf-interfaces": + sys.exit(ietf_interfaces(json_data, args.name)) + else: + print(f"Error, unknown module {args.module}") + sys.exit(1) -if args.module == "ietf-interfaces": - sys.exit(ietf_interfaces(json, args.name)) -else: - print(f"Error, unknown module {args.module}") - sys.exit(1) +if __name__ == "__main__": + main()