Skip to content

Commit dc3fef6

Browse files
committed
fix: better error handling for deserialisation
1 parent 1d0fac0 commit dc3fef6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

alpacloud/crdvis/crd.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import requests
99
import yaml
10+
from pydantic import ValidationError
1011

1112
from alpacloud.crdvis.models import CustomResourceDefinition
1213

@@ -70,5 +71,15 @@ def read_path(path: str) -> CustomResourceDefinition:
7071

7172
if not content:
7273
raise CRDReadError("Empty content")
73-
doc = yaml.safe_load(content)
74-
return CustomResourceDefinition.model_validate(doc)
74+
75+
try:
76+
doc = yaml.safe_load(content)
77+
return CustomResourceDefinition.model_validate(doc)
78+
except yaml.YAMLError as e:
79+
raise CRDReadError(f"CRD could not be loaded as YAML: {e}")
80+
except ValidationError as e:
81+
# escaping pydantic help message
82+
# like "Input should be a valid dictionary or instance of CustomResourceDefinition [type=model_type, input_value='applications.argoproj.io', input_type=str]"
83+
# for rich
84+
msg = str(e).replace("[", r"\[")
85+
raise CRDReadError(f"CRD could not be validated: {msg}")

0 commit comments

Comments
 (0)