Source code for sqlspec.exceptions

from collections.abc import Generator
from contextlib import contextmanager
from typing import Any, Final

__all__ = (
    "SQLSTATE_EXCEPTION_MAP",
    "CheckViolationError",
    "ConfigResolverError",
    "ConnectionTimeoutError",
    "DataError",
    "DatabaseConnectionError",
    "DeadlockError",
    "DialectNotSupportedError",
    "EventChannelError",
    "FileNotFoundInStorageError",
    "ForeignKeyViolationError",
    "ImproperConfigurationError",
    "IntegrityError",
    "InvalidVersionFormatError",
    "MigrationError",
    "MissingDependencyError",
    "MultipleResultsFoundError",
    "NotFoundError",
    "NotNullViolationError",
    "OperationalError",
    "OutOfOrderMigrationError",
    "PermissionDeniedError",
    "QueryTimeoutError",
    "RepositoryError",
    "SQLBuilderError",
    "SQLConversionError",
    "SQLFileNotFoundError",
    "SQLFileParseError",
    "SQLParsingError",
    "SQLSpecError",
    "SQLStatementNotFoundError",
    "SerializationConflictError",
    "SerializationError",
    "SquashValidationError",
    "StackExecutionError",
    "StorageCapabilityError",
    "StorageOperationFailedError",
    "TransactionError",
    "TransactionRetryError",
    "UniqueViolationError",
    "map_sqlstate_to_exception",
)

STACK_SQL_PREVIEW_LIMIT: Final[int] = 120


class SQLSpecError(Exception):
    """Base exception class for SQLSpec exceptions."""

    detail: str = ""

    def __init__(self, *args: Any, detail: str = "") -> None:
        """Initialize SQLSpecError.

        Args:
            *args: args are converted to :class:`str` before passing to :class:`Exception`
            detail: detail of the exception.
        """
        str_args = [str(arg) for arg in args if arg]
        if not detail:
            detail = str_args[0] if str_args else ""
        self.detail = detail
        if detail and detail not in str_args:
            str_args = [detail, *str_args]
        super().__init__(*str_args)
def __repr__(self) -> str: if self.detail: return f"{self.__class__.__name__} - {self.detail}" return self.__class__.__name__ def __str__(self) -> str: parts = list(self.args) if self.detail and self.detail not in self.args: parts.append(self.detail) return " ".join(parts).strip()