Skip to content

Commit 5fcaac9

Browse files
authored
Merge pull request #81 from olehermanse/main
cfengine format: Added detection of invalid JSON
2 parents 5ec0094 + ef404a7 commit 5fcaac9

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import IO
44

5+
import json
6+
57
import tree_sitter_cfengine as tscfengine
68
from tree_sitter import Language, Parser, Node
79
from cfbs.pretty import pretty_file, pretty_check_file
@@ -46,10 +48,14 @@ def format_json_file(filename: str, check: bool) -> int:
4648
print(f"JSON file '{filename}' needs reformatting")
4749
return int(not success)
4850

49-
reformatted = pretty_file(filename)
50-
if reformatted:
51-
print(f"JSON file '{filename}' was reformatted")
52-
return 0 # Successfully reformatted or no reformat needed
51+
try:
52+
reformatted = pretty_file(filename)
53+
if reformatted:
54+
print(f"JSON file '{filename}' was reformatted")
55+
return 0 # Successfully reformatted or no reformat needed
56+
except json.decoder.JSONDecodeError as e:
57+
print(f"JSON file '{filename}' invalid ({str(e)})")
58+
return 1
5359

5460

5561
def text(node: Node) -> str:

0 commit comments

Comments
 (0)