Coverage for src / lexigram / contracts / data / exceptions.py: 4%
27 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-19 05:41 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-19 05:41 +0800
1"""Exceptions for the data access layer contracts."""
3from __future__ import annotations
5from typing import Any
7from lexigram.contracts.exceptions.base import LexigramError
8from lexigram.contracts.exceptions.domain import DomainError
11class UnitOfWorkError(LexigramError):
12 """Raised when a unit-of-work operation fails or is used incorrectly."""
14 _code = "LEX_ERR_DB_006"
16 def __init__(self, message: str = "Unit of work error", **kwargs: Any) -> None:
17 super().__init__(message, **kwargs)
20class DataError(DomainError):
21 """Base exception for all data-domain errors.
23 This is an expected, recoverable error that data clients should
24 handle gracefully (e.g., retry, return not found, etc.).
25 """
27 _code = "LEX_ERR_DB_007"
29 def __init__(self, message: str = "Data error", **kwargs: Any) -> None:
30 super().__init__(message, **kwargs)
33class DataNotFoundError(DataError):
34 """Raised when data item is not found."""
36 _code = "LEX_ERR_DB_008"
38 def __init__(self, item_type: str, item_id: str, **kwargs: Any) -> None:
39 self.item_type = item_type
40 self.item_id = item_id
41 message = f"{item_type} {item_id} not found"
42 super().__init__(message, **kwargs)
45class DataValidationError(DataError):
46 """Raised when data validation fails."""
48 _code = "LEX_ERR_DB_009"
50 def __init__(
51 self,
52 message: str,
53 field: str | None = None,
54 **kwargs: Any,
55 ) -> None:
56 self.message = message
57 self.field = field
58 full_msg = f"{message}" if not field else f"{message} (field: {field})"
59 super().__init__(full_msg, **kwargs)
62__all__ = ["DataError", "DataNotFoundError", "DataValidationError", "UnitOfWorkError"]