Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix confusing ReadError traceback #262

Closed
wants to merge 3 commits into from

Conversation

ssbarnea
Copy link
Contributor

@ssbarnea ssbarnea commented May 2, 2020

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.

Example output after this change:

bad-file.yaml:4:36: [error] syntax error: special characters are not allowed (syntax)

Fixes: #261

@coveralls
Copy link

coveralls commented May 2, 2020

Coverage Status

Coverage decreased (-1.0%) to 96.627% when pulling dd09ea2 on ssbarnea:master into 2f42311 on adrienverge:master.

@ssbarnea ssbarnea force-pushed the master branch 2 times, most recently from b161159 to ed3946b Compare May 2, 2020 16:11
@ssbarnea ssbarnea marked this pull request as ready for review May 2, 2020 16:15
@ssbarnea ssbarnea force-pushed the master branch 2 times, most recently from 3117451 to 85bae6e Compare May 3, 2020 15:30
@ssbarnea
Copy link
Contributor Author

ssbarnea commented May 3, 2020

@adrienverge Does this look ok?

@adrienverge
Copy link
Owner

Wow, reviving a PR after one day is a bit too much. I have concerns about this commit, I'll take the time to think about it. Please be patient.

yamllint/linter.py Outdated Show resolved Hide resolved
Copy link
Owner

@adrienverge adrienverge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ssbarnea!

My initial preferred solution was to re-raise an error that would contain both the problematic file name and the precise exception encountered:

    try:
        syntax_error = get_syntax_error(buffer)
    except yaml.reader.ReaderError as e:
        raise Exception('Read error on file ' + filepath) from e

But your proposal is more user-friendly, as it reports the error without stopping.

Can you add tests and fix the small problem below? Thanks


def test_non_printable_characters(self):
self.check('output: \x1b\n',
None, problem=(1, 9))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more tests. At the least I can think of:

  • invalid character in the first byte,
  • invalid character in the last byte,
  • invalid character not on line 1,
  • multiple invalid characters,
  • test with other rules enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long delay, somehow this patch got lost and only rediscovered it yesterday. I added the tests.

Still, I am unsure if the coverage failure is a bug in coverage because these new tests would cover that piece of code. Also there are no instructions about running coverage locally for the project so I hope you may be able to provide few hints here.

yamllint/linter.py Outdated Show resolved Hide resolved
@ssbarnea ssbarnea changed the title Fix confusing ReadError traceback WIP: Fix confusing ReadError traceback Aug 20, 2020
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: adrienverge#261
@adrienverge adrienverge changed the title WIP: Fix confusing ReadError traceback Fix confusing ReadError traceback Oct 12, 2020
Copy link
Owner

@adrienverge adrienverge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this test? Can you reintroduce it?

    def test_non_printable_characters(self):
        self.check('output: \x1b\n',
                   None, problem=(1, 9))

tests/test_syntax_errors.py Outdated Show resolved Hide resolved
tests/test_syntax_errors.py Show resolved Hide resolved
@ssbarnea ssbarnea closed this Jan 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReadError exception fails to mention the file generating the error.
4 participants