Skip to content

Commit

Permalink
Merge pull request #1543 from asottile/filename-in-execution-error
Browse files Browse the repository at this point in the history
include the file path in the plugin execution error
  • Loading branch information
asottile committed Jan 24, 2022
2 parents e704ab4 + d2333c4 commit f178bd3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/flake8/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ def run_check(self, plugin: LoadedPlugin, **arguments: Any) -> Any:
exc_info=True,
)
raise exceptions.PluginExecutionFailed(
plugin_name=plugin.display_name, exception=all_exc
filename=self.filename,
plugin_name=plugin.display_name,
exception=all_exc,
)

@staticmethod
Expand Down
23 changes: 15 additions & 8 deletions src/flake8/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,24 @@ def __str__(self) -> str:
class PluginExecutionFailed(Flake8Exception):
"""The plugin failed during execution."""

FORMAT = '"%(name)s" failed during execution due to "%(exc)s"'

def __init__(self, plugin_name: str, exception: Exception) -> None:
FORMAT = '{fname}: "{plugin}" failed during execution due to {exc!r}'

def __init__(
self,
filename: str,
plugin_name: str,
exception: Exception,
) -> None:
"""Utilize keyword arguments for message generation."""
self.filename = filename
self.plugin_name = plugin_name
self.original_exception = exception
super().__init__(plugin_name, exception)
super().__init__(filename, plugin_name, exception)

def __str__(self) -> str:
"""Format our exception message."""
return self.FORMAT % {
"name": self.plugin_name,
"exc": self.original_exception,
}
return self.FORMAT.format(
fname=self.filename,
plugin=self.plugin_name,
exc=self.original_exception,
)
1 change: 1 addition & 0 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
exception=ValueError("boom!"),
),
exceptions.PluginExecutionFailed(
filename="filename.py",
plugin_name="plugin_name",
exception=ValueError("boom!"),
),
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/test_file_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ def test_raises_exception_on_failed_plugin(tmp_path, default_options):
plugins=finder.Checkers([], [], []),
options=default_options,
)
with pytest.raises(flake8.exceptions.PluginExecutionFailed):
with pytest.raises(flake8.exceptions.PluginExecutionFailed) as excinfo:
fchecker.run_check(plugin)
expected = (
f'{fname}: "plugin-name[X]" failed during execution '
f"due to ValueError()"
)
assert str(excinfo.value) == expected

0 comments on commit f178bd3

Please sign in to comment.