config-skeleton/config_skeleton/exceptions.py

55 lines
1.5 KiB
Python
Raw Normal View History

2021-05-22 18:30:24 +02:00
"""Common exceptions raised by the program."""
from typing import List, Union
2021-05-22 18:30:24 +02:00
class InvalidKeyError(Exception):
"""Thrown when a config file contains an invalid key."""
def __init__(self, keys: Union[str, List[str]]):
"""Create a new InvalidKeyError object with the given key.
Args:
keys: the invalid key(s)
"""
if type(keys) == str:
keys = [keys]
self.message = "Invalid key(s): {}".format(", ".join(keys))
super().__init__()
class MissingKeyError(Exception):
"""Thrown when a required key is missing from a config."""
def __init__(self, keys: Union[str, List[str]]):
"""Create a new MissingKeyError object with the given key.
Args:
keys: the invalid key(s)
"""
if type(keys) == str:
keys = [keys]
self.message = "Missing key(s): {}".format(", ".join(keys))
super().__init__()
class InvalidValueError(Exception):
"""Thrown when a key contains an invalid value."""
def __init__(self, key: str, expected: str, actual: str):
"""Create a new InvalidValueError given the arguments.
Args:
key: the key containing the invalid value
expected: name of the expected type
actual: name of the actual type
"""
self.message = (
2021-05-22 21:28:01 +02:00
f"Invalid value for key {key}: expected {expected}, " f"got {actual}"
2021-05-22 18:30:24 +02:00
)
super().__init__()