exceptions.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Exception classes for all of Flake8."""
  2. class Flake8Exception(Exception):
  3. """Plain Flake8 exception."""
  4. class EarlyQuit(Flake8Exception):
  5. """Except raised when encountering a KeyboardInterrupt."""
  6. class ExecutionError(Flake8Exception):
  7. """Exception raised during execution of Flake8."""
  8. class FailedToLoadPlugin(Flake8Exception):
  9. """Exception raised when a plugin fails to load."""
  10. FORMAT = 'Flake8 failed to load plugin "%(name)s" due to %(exc)s.'
  11. def __init__(self, plugin_name: str, exception: Exception) -> None:
  12. """Initialize our FailedToLoadPlugin exception."""
  13. self.plugin_name = plugin_name
  14. self.original_exception = exception
  15. super().__init__(plugin_name, exception)
  16. def __str__(self) -> str:
  17. """Format our exception message."""
  18. return self.FORMAT % {
  19. "name": self.plugin_name,
  20. "exc": self.original_exception,
  21. }
  22. class PluginRequestedUnknownParameters(Flake8Exception):
  23. """The plugin requested unknown parameters."""
  24. FORMAT = '"%(name)s" requested unknown parameters causing %(exc)s'
  25. def __init__(self, plugin_name: str, exception: Exception) -> None:
  26. """Pop certain keyword arguments for initialization."""
  27. self.plugin_name = plugin_name
  28. self.original_exception = exception
  29. super().__init__(plugin_name, exception)
  30. def __str__(self) -> str:
  31. """Format our exception message."""
  32. return self.FORMAT % {
  33. "name": self.plugin_name,
  34. "exc": self.original_exception,
  35. }
  36. class PluginExecutionFailed(Flake8Exception):
  37. """The plugin failed during execution."""
  38. FORMAT = '{fname}: "{plugin}" failed during execution due to {exc!r}'
  39. def __init__(
  40. self,
  41. filename: str,
  42. plugin_name: str,
  43. exception: Exception,
  44. ) -> None:
  45. """Utilize keyword arguments for message generation."""
  46. self.filename = filename
  47. self.plugin_name = plugin_name
  48. self.original_exception = exception
  49. super().__init__(filename, plugin_name, exception)
  50. def __str__(self) -> str:
  51. """Format our exception message."""
  52. return self.FORMAT.format(
  53. fname=self.filename,
  54. plugin=self.plugin_name,
  55. exc=self.original_exception,
  56. )