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

1"""Exceptions for the data access layer contracts.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6 

7from lexigram.contracts.exceptions.base import LexigramError 

8from lexigram.contracts.exceptions.domain import DomainError 

9 

10 

11class UnitOfWorkError(LexigramError): 

12 """Raised when a unit-of-work operation fails or is used incorrectly.""" 

13 

14 _code = "LEX_ERR_DB_006" 

15 

16 def __init__(self, message: str = "Unit of work error", **kwargs: Any) -> None: 

17 super().__init__(message, **kwargs) 

18 

19 

20class DataError(DomainError): 

21 """Base exception for all data-domain errors. 

22 

23 This is an expected, recoverable error that data clients should 

24 handle gracefully (e.g., retry, return not found, etc.). 

25 """ 

26 

27 _code = "LEX_ERR_DB_007" 

28 

29 def __init__(self, message: str = "Data error", **kwargs: Any) -> None: 

30 super().__init__(message, **kwargs) 

31 

32 

33class DataNotFoundError(DataError): 

34 """Raised when data item is not found.""" 

35 

36 _code = "LEX_ERR_DB_008" 

37 

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) 

43 

44 

45class DataValidationError(DataError): 

46 """Raised when data validation fails.""" 

47 

48 _code = "LEX_ERR_DB_009" 

49 

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) 

60 

61 

62__all__ = ["DataError", "DataNotFoundError", "DataValidationError", "UnitOfWorkError"]