Skip to content

Commit

Permalink
Fix confusing ReadError traceback
Browse files Browse the repository at this point in the history
Avoids confusing tracebacks when ReadErrors exceptions are raised
from the yaml reader.

Now we do print the filename that failed to load and stop processing
in order to avoid additional exceptions.

Also adds tests to prevent future regressions.

Fixes: #261
  • Loading branch information
ssbarnea committed May 2, 2020
1 parent 512fe17 commit b161159
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tests/test_syntax_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ def test_sets(self):
' a:\n'
' set\n'
'...\n', None)

def test_non_printable_characters(self):
self.check('output: |\n'
' ERROR: text with ANSI escapes)',
None, problem=(22, 0))
15 changes: 12 additions & 3 deletions yamllint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def get_syntax_error(buffer):
'syntax error: ' + e.problem + ' (syntax)')
problem.level = 'error'
return problem
except yaml.reader.ReaderError as e:
problem = LintProblem(e.position + 1,
0,
'syntax error: ' + e.reason + ' (syntax)')
problem.level = 'error'
return problem


def _run(buffer, conf, filepath):
Expand All @@ -197,6 +203,12 @@ def _run(buffer, conf, filepath):
# right line
syntax_error = get_syntax_error(buffer)

# syntax errors include failure to load the file and we should not do
# any additional processing after if one is encountered.
if syntax_error:
yield syntax_error
return

for problem in get_cosmetic_problems(buffer, conf, filepath):
# Insert the syntax error (if any) at the right place...
if (syntax_error and syntax_error.line <= problem.line and
Expand All @@ -215,9 +227,6 @@ def _run(buffer, conf, filepath):

yield problem

if syntax_error:
yield syntax_error


def run(input, conf, filepath=None):
"""Lints a YAML source.
Expand Down

0 comments on commit b161159

Please sign in to comment.