diff --git a/src/cihai/_internal/config_reader.py b/src/cihai/_internal/config_reader.py index bcc6db03..94b1e371 100644 --- a/src/cihai/_internal/config_reader.py +++ b/src/cihai/_internal/config_reader.py @@ -12,6 +12,16 @@ FormatLiteral = t.Literal["json", "yaml"] +class ConfigFormatNotImplementedError(NotImplementedError): + def __init__(self, format: str): + return super().__init__(f"{format} not supported in configuration") + + +class ConfigExtensionNotImplementedError(NotImplementedError): + def __init__(self, ext: str, path: str | pathlib.Path): + return super().__init__(f"{ext} not supported in {path}") + + class ConfigReader: r"""Parse string data (YAML and JSON) into a dictionary. @@ -46,7 +56,7 @@ def _load(format: "FormatLiteral", content: str) -> dict[str, t.Any]: elif format == "json": return t.cast(dict[str, t.Any], json.loads(content)) else: - raise NotImplementedError(f"{format} not supported in configuration") + raise NotImplementedError(format=format) @classmethod def load(cls, format: "FormatLiteral", content: str) -> "ConfigReader": @@ -109,7 +119,7 @@ def _from_file(cls, path: pathlib.Path) -> dict[str, t.Any]: elif path.suffix == ".json": format = "json" else: - raise NotImplementedError(f"{path.suffix} not supported in {path}") + raise ConfigExtensionNotImplementedError(ext=path.suffix, path=path) return cls._load( format=format, @@ -184,7 +194,7 @@ def _dump( indent=2, ) else: - raise NotImplementedError(f"{format} not supported in config") + raise ConfigFormatNotImplementedError(format=format) def dump(self, format: "FormatLiteral", indent: int = 2, **kwargs: t.Any) -> str: r"""Dump via ConfigReader instance. diff --git a/src/cihai/core.py b/src/cihai/core.py index 38b60231..06c4e3aa 100644 --- a/src/cihai/core.py +++ b/src/cihai/core.py @@ -25,6 +25,11 @@ log = logging.getLogger(__name__) +class CihaiConfigError(exc.CihaiException): + def __init__(self): + return super().__init__("Invalid exception with configuration") + + def is_valid_config(config: "UntypedDict") -> "TypeGuard[ConfigDict]": return True @@ -101,7 +106,7 @@ def __init__( expand_config(_config, app_dirs) if not is_valid_config(config=_config): - raise exc.CihaiException("Invalid exception with configuration") + raise CihaiConfigError() self.config = _config