Skip to content

Commit 0772e3e

Browse files
committed
Improve error reporting for object validate command
1 parent 3e52900 commit 0772e3e

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

infrahub_sdk/ctl/object.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..async_typer import AsyncTyper
88
from ..ctl.client import initialize_client
99
from ..ctl.utils import catch_exception, init_logging
10+
from ..exceptions import ValidationError
1011
from ..spec.object import ObjectFile
1112
from .parameters import CONFIG_PARAM
1213
from .utils import load_yamlfile_from_disk_and_exit
@@ -39,8 +40,21 @@ async def load(
3940
files = load_yamlfile_from_disk_and_exit(paths=paths, file_type=ObjectFile, console=console)
4041
client = initialize_client()
4142

43+
has_errors = False
44+
4245
for file in files:
43-
await file.validate_format(client=client, branch=branch)
46+
try:
47+
await file.validate_format(client=client, branch=branch)
48+
except ValidationError as e:
49+
has_errors = True
50+
if file.multiple_documents:
51+
console.print(f"[red] File '{file.location}' [{file.document_position}] is not valid!")
52+
else:
53+
console.print(f"[red] File '{file.location}' is not valid!")
54+
console.print(f"[red] {e.message}")
55+
56+
if has_errors:
57+
raise typer.Exit(1)
4458

4559
for file in files:
4660
await file.process(client=client, branch=branch)
@@ -63,5 +77,21 @@ async def validate(
6377
files = load_yamlfile_from_disk_and_exit(paths=paths, file_type=ObjectFile, console=console)
6478
client = initialize_client()
6579

80+
has_errors = False
6681
for file in files:
67-
await file.validate_format(client=client, branch=branch)
82+
try:
83+
await file.validate_format(client=client, branch=branch)
84+
if file.multiple_documents:
85+
console.print(f"[green] File '{file.location}' [{file.document_position}] is Valid!")
86+
else:
87+
console.print(f"[green] File '{file.location}' is Valid!")
88+
except ValidationError as e:
89+
has_errors = True
90+
if file.multiple_documents:
91+
console.print(f"[red] File '{file.location}' [{file.document_position}] is not valid!")
92+
else:
93+
console.print(f"[red] File '{file.location}' is not valid!")
94+
console.print(f"[red] {e.message}")
95+
96+
if has_errors:
97+
raise typer.Exit(1)

0 commit comments

Comments
 (0)