Skip to content

Errors Handling

ormdantic.errors.OrmdanticError

OrmdanticError(msg, *, context=None, cause=None)

Bases: ValueError

Base class for typed Ormdantic runtime errors.

The string message stays concise for users while structured metadata is available on context for logging and support tooling.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.ConfigurationError

ConfigurationError(msg)

Bases: OrmdanticError

Raised for mal-configured database models or schemas.

Source code in ormdantic/errors.py
def __init__(self, msg: str):
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.QueryCompilationError

QueryCompilationError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised when Ormdantic cannot compile a query.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.QueryExecutionError

QueryExecutionError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised when a compiled query fails during execution.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.DatabaseConnectionError

DatabaseConnectionError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised when the native runtime cannot connect to the database.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.SchemaError

SchemaError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised for schema creation, validation, or DDL failures.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.MigrationError

MigrationError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised for migration planning, history, apply, or rollback failures.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.ReflectionError

ReflectionError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised when database reflection fails.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.RelationshipLoadingError

RelationshipLoadingError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised when eager or explicit relationship loading fails.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.HydrationError

HydrationError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised when native rows cannot be hydrated into model instances.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.TransactionError

TransactionError(msg, *, context=None, cause=None)

Bases: OrmdanticError

Raised for transaction, savepoint, commit, or rollback failures.

Source code in ormdantic/errors.py
def __init__(
    self,
    msg: str,
    *,
    context: Mapping[str, Any] | None = None,
    cause: BaseException | None = None,
) -> None:
    self.context = dict(context or {})
    self.cause = cause
    self.native_message = str(cause) if cause is not None else None
    self.native_error_type = type(cause).__name__ if cause is not None else None
    if self.native_message is not None:
        self.context.setdefault("native_message", self.native_message)
        self.context.setdefault("native_error_type", self.native_error_type)
    super().__init__(msg)

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.UndefinedBackReferenceError

UndefinedBackReferenceError(table_a, table_b, field)

Bases: ConfigurationError

Raised when a back reference is missing from a table.

Source code in ormdantic/errors.py
def __init__(self, table_a: str, table_b: str, field: str) -> None:
    super().__init__(
        f'Many relation defined on "{table_a}.{field}" to table {table_b}" must be'
        f' defined with a back reference on "{table_a}".'
    )

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.MismatchingBackReferenceError

MismatchingBackReferenceError(
    table_a, table_b, field, back_reference
)

Bases: ConfigurationError

Raised when a back reference is typed incorrectly.

Source code in ormdantic/errors.py
def __init__(
    self, table_a: str, table_b: str, field: str, back_reference: str
) -> None:
    super().__init__(
        f'Many relation defined on "{table_a}.{field}" to'
        f' "{table_b}.{back_reference}" must use the same model type'
        f" back-referenced."
    )

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.MustUnionForeignKeyError

MustUnionForeignKeyError(
    table_a, table_b, field, model_b, pk_type
)

Bases: ConfigurationError

Raised when a relation field doesn't allow for just foreign key.

Source code in ormdantic/errors.py
def __init__(
    self,
    table_a: str,
    table_b: str,
    field: str,
    model_b: Type,  # type: ignore
    pk_type: Type,  # type: ignore
) -> None:
    super().__init__(
        f'Relation defined on "{table_a}.{field}" to "{table_b}" must be a union'
        f' type of "Model | model_pk_type" e.g. "{model_b.__name__} | {pk_type}"'
    )

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self

ormdantic.errors.TypeConversionError

TypeConversionError(type)

Bases: ConfigurationError

Raised when a Python type fails to convert to SQL.

Source code in ormdantic/errors.py
def __init__(self, type: Type) -> None:  # type: ignore
    super().__init__(f"Type {type} is not supported by Ormdantic.")

context instance-attribute

context = dict(context or {})

cause instance-attribute

cause = cause

native_message instance-attribute

native_message = str(cause) if cause is not None else None

native_error_type instance-attribute

native_error_type = __name__ if cause is not None else None

with_context

with_context(**context)

Attach additional context without replacing existing keys.

Source code in ormdantic/errors.py
def with_context(self: ErrorT, **context: Any) -> ErrorT:
    """Attach additional context without replacing existing keys."""
    for key, value in context.items():
        if value is not None:
            self.context.setdefault(key, value)
    return self