Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

3.0 KB
Raw Blame
"""Custom exception classes for the settings module."""

from collections.abc import Sequence
from pathlib import Path
from typing import Any


class SettingsValueError(ValueError):
    """A configured setting cannot be read safely.

    Include only the setting key, source and a fixed diagnostic reason. Values
    and the exceptions raised while parsing them may contain credentials.
    """

    def __init__(self, key: str, *, source: str, reason: str):
        self.key = key
        self.source = source
        self.reason = reason
        super().__init__(self.diagnostic)

    @property
    def diagnostic(self) -> str:
        """Return the setting key, its source and the reason, without a value.

        HTTP handlers return this text so an operator can locate the setting.
        It is rebuilt from the three fields rather than taken from the
        exception's arguments.
        """
        return f"Invalid setting {self.key!r} from {self.source}: {self.reason}"


class EnvSettingError(ValueError):
    """Base class for environment setting errors."""

    def __init__(self, env_var: str, message: str):
        self.env_var = env_var
        super().__init__(f"{env_var}: {message}")


class MissingEnvironmentVariableError(EnvSettingError):
    """Raised when a required environment variable is not set."""

    def __init__(self, env_var: str):
        super().__init__(env_var, "Required environment variable is not set")


class EnvironmentValueRangeError(EnvSettingError):
    """Raised when an environment variable value is out of the allowed range."""

    def __init__(
        self, env_var: str, value: Any, min_val: Any = None, max_val: Any = None
    ):
        if min_val is not None and value < min_val:
            msg = f"value {value} is below minimum {min_val}"
        elif max_val is not None and value > max_val:
            msg = f"value {value} is above maximum {max_val}"
        else:
            msg = f"value {value} is out of range"
        super().__init__(env_var, msg)


class EnvironmentPathNotFoundError(EnvSettingError):
    """Raised when a path specified in an environment variable does not exist."""

    def __init__(self, env_var: str, path: Path | str):
        super().__init__(env_var, f"Path {path} does not exist")


class InvalidEnvironmentValueError(EnvSettingError):
    """Raised when an environment variable value fails its typed validation."""

    def __init__(
        self,
        env_var: str,
        value: str,
        allowed_values: Sequence[str] | None = None,
        expected_type: str | None = None,
    ):
        if expected_type is not None:
            message = "value '{}' is not a valid {}".format(
                value, expected_type
            )
        elif allowed_values is not None:
            message = "value '{}' not in allowed values: {}".format(
                value, allowed_values
            )
        else:
            message = "value '{}' is invalid".format(value)
        super().__init__(env_var, message)