From ef404a7e5503d4f2584fe4f610496de9c23c332e Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Wed, 22 Apr 2026 13:31:25 +0200 Subject: [PATCH] cfengine format: Added detection of invalid JSON Signed-off-by: Ole Herman Schumacher Elgesem --- src/cfengine_cli/format.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/cfengine_cli/format.py b/src/cfengine_cli/format.py index bb13648..c571003 100644 --- a/src/cfengine_cli/format.py +++ b/src/cfengine_cli/format.py @@ -2,6 +2,8 @@ from typing import IO +import json + import tree_sitter_cfengine as tscfengine from tree_sitter import Language, Parser, Node from cfbs.pretty import pretty_file, pretty_check_file @@ -46,10 +48,14 @@ def format_json_file(filename: str, check: bool) -> int: print(f"JSON file '{filename}' needs reformatting") return int(not success) - reformatted = pretty_file(filename) - if reformatted: - print(f"JSON file '{filename}' was reformatted") - return 0 # Successfully reformatted or no reformat needed + try: + reformatted = pretty_file(filename) + if reformatted: + print(f"JSON file '{filename}' was reformatted") + return 0 # Successfully reformatted or no reformat needed + except json.decoder.JSONDecodeError as e: + print(f"JSON file '{filename}' invalid ({str(e)})") + return 1 def text(node: Node) -> str: