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