exceptions.py 2.3 KB

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