Skip to content

Commit b2c9c2c

Browse files
fmoessbauerjan-kiszka
authored andcommitted
includehandler: improve reporting of yaml format errors
When passing a yaml file that is incorrectly formatted, the parser raises an error. This error needs to be caught, as otherwise a huge splat is given back to the user which hides the error. We now handle this case and report the line at which the error most likely occurred. Reported-by: Jan Kiszka <[email protected]> Signed-off-by: Felix Moessbauer <[email protected]> Signed-off-by: Jan Kiszka <[email protected]>
1 parent 4c7a0f5 commit b2c9c2c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

kas/includehandler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,15 @@ def load_config(filename):
6969
with open(filename, 'rb') as fds:
7070
config = json.load(fds)
7171
elif ext in ['.yml', '.yaml']:
72-
with open(filename, 'rb') as fds:
73-
config = yaml.safe_load(fds)
72+
try:
73+
with open(filename, 'rb') as fds:
74+
config = yaml.safe_load(fds)
75+
except yaml.YAMLError as e:
76+
msg = f'Error in line {e.problem_mark.line + 1}' \
77+
if hasattr(e, 'problem_mark') else ''
78+
raise LoadConfigException(
79+
f'Configuration file is not valid YAML: {msg}',
80+
filename)
7481
else:
7582
raise LoadConfigException('Config file extension not recognized',
7683
filename)

tests/test_includehandler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def test_err_version_invalid_format(self):
120120

121121
self.util_exception_content(testvector)
122122

123+
def test_err_parse_yaml(self):
124+
exception = includehandler.LoadConfigException
125+
testvector = [
126+
# misaligned column
127+
('header:\n version: 17\n repo:', exception),
128+
]
129+
self.util_exception_content(testvector)
130+
123131
def test_header_valid(self):
124132
testvector = [
125133
'header: {version: 4}',

0 commit comments

Comments
 (0)