Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-session/src/lexigram/ai/session/exceptions.py: 64%

36 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 07:19 +0800

1"""Errors for the lexigram-ai-session package.""" 

2 

3from __future__ import annotations 

4 

5from lexigram.contracts.ai.session import SessionError as _ContractsSessionError 

6 

7 

8class SessionError(_ContractsSessionError): 

9 """Base error for all session operations. 

10 

11 All session-specific exceptions inherit from this class. 

12 """ 

13 

14 _code: str = "LEX_ERR_SES_006" 

15 

16 

17class SessionNotFoundError(SessionError): 

18 """Raised when a session cannot be found by its ID. 

19 

20 Args: 

21 session_id: The ID of the session that was not found. 

22 """ 

23 

24 _code: str = "LEX_ERR_SES_007" 

25 

26 def __init__(self, session_id: str) -> None: 

27 self.session_id = session_id 

28 super().__init__(f"Session not found: {session_id!r}") 

29 

30 

31class SessionClosedError(SessionError): 

32 """Raised when an operation is attempted on a closed session. 

33 

34 Args: 

35 session_id: The ID of the closed session. 

36 """ 

37 

38 _code: str = "LEX_ERR_SES_008" 

39 

40 def __init__(self, session_id: str) -> None: 

41 self.session_id = session_id 

42 super().__init__(f"Session is closed and cannot be modified: {session_id!r}") 

43 

44 

45class SessionExpiredError(SessionError): 

46 """Raised when an operation is attempted on an expired session. 

47 

48 Args: 

49 session_id: The ID of the expired session. 

50 """ 

51 

52 _code: str = "LEX_ERR_SES_009" 

53 

54 def __init__(self, session_id: str) -> None: 

55 self.session_id = session_id 

56 super().__init__(f"Session has expired: {session_id!r}") 

57 

58 

59class CheckpointNotFoundError(SessionError): 

60 """Raised when a checkpoint cannot be found by its ID. 

61 

62 Args: 

63 checkpoint_id: The ID of the checkpoint that was not found. 

64 """ 

65 

66 _code: str = "LEX_ERR_SES_010" 

67 

68 def __init__(self, checkpoint_id: str) -> None: 

69 self.checkpoint_id = checkpoint_id 

70 super().__init__(f"Checkpoint not found: {checkpoint_id!r}") 

71 

72 

73class SessionTransitionError(SessionError): 

74 """Raised when an invalid state transition is attempted. 

75 

76 Args: 

77 session_id: The session ID. 

78 from_status: Current status. 

79 to_status: Attempted target status. 

80 """ 

81 

82 _code: str = "LEX_ERR_SES_011" 

83 

84 def __init__(self, session_id: str, from_status: str, to_status: str) -> None: 

85 self.session_id = session_id 

86 self.from_status = from_status 

87 self.to_status = to_status 

88 super().__init__( 

89 f"Invalid transition for session {session_id!r}: " 

90 f"{from_status!r}{to_status!r}" 

91 ) 

92 

93 

94class SessionCapacityError(SessionError): 

95 """Raised when a session limit is exceeded. 

96 

97 Args: 

98 detail: Human-readable description of the exceeded limit. 

99 """ 

100 

101 _code: str = "LEX_ERR_SES_012" 

102 

103 def __init__(self, detail: str) -> None: 

104 super().__init__(f"Session capacity exceeded: {detail}") 

105 

106 

107__all__ = [ 

108 "CheckpointNotFoundError", 

109 "SessionCapacityError", 

110 "SessionClosedError", 

111 "SessionError", 

112 "SessionExpiredError", 

113 "SessionNotFoundError", 

114 "SessionTransitionError", 

115]