-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.py
33 lines (22 loc) · 1.09 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Exceptions for custom input types / validators.
:author: Doug Skrypa
"""
from typing import Collection, Any
from ..exceptions import CommandParserException
__all__ = ['InputValidationError', 'InvalidChoiceError']
class InputValidationError(CommandParserException, ValueError):
"""Raised when a custom InputType's conversion/validation fails"""
class InvalidChoiceError(InputValidationError):
"""Error raised when a value that does not match one of the pre-defined choices was provided"""
def __init__(self, invalid: Any, choices: Collection[Any], type_str: str = 'choice'): # pylint: disable=W0231
self.invalid = invalid
self.choices = choices
self.type_str = type_str
def __str__(self) -> str:
if isinstance(self.invalid, Collection) and not isinstance(self.invalid, str):
bad_str = f'{self.type_str}s: {", ".join(map(repr, self.invalid))}'
else:
bad_str = f'{self.type_str}: {self.invalid!r}'
choices_str = ', '.join(map(repr, self.choices))
return f'invalid {bad_str} (choose from: {choices_str})'