Coverage for snekql/errors.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-07 21:13 +0300

1"""Intentional package-originated exception hierarchy for snekql.""" 

2 

3from __future__ import annotations 

4 

5 

6class SnekqlError(Exception): 

7 """Base class for all intentional package-originated exceptions. 

8 

9 >>> isinstance(ModelDeclarationError("bad model"), SnekqlError) 

10 True 

11 """ 

12 

13 

14class ModelError(SnekqlError): 

15 """Base class for table model declaration and validation failures.""" 

16 

17 

18class ModelDeclarationError(ModelError): 

19 """Raised when a table model class violates snekql declaration rules.""" 

20 

21 

22class ModelValidationError(ModelError): 

23 """Raised when pending or fetched table model values fail validation.""" 

24 

25 

26class FrozenModelError(ModelError): 

27 """Raised when code attempts to mutate an immutable table model instance.""" 

28 

29 

30class QueryError(SnekqlError): 

31 """Base class for query builder construction and compilation failures.""" 

32 

33 

34class QueryConstructionError(QueryError): 

35 """Raised when query builder methods are used in an invalid sequence.""" 

36 

37 

38class QueryCompilationError(QueryError): 

39 """Raised when a built query cannot be compiled into valid SQLite SQL.""" 

40 

41 

42class DatabaseRuntimeError(SnekqlError): 

43 """Base class for Database and Transaction execution failures.""" 

44 

45 

46class DatabaseClosedError(DatabaseRuntimeError): 

47 """Raised when a closed Database is used for new work.""" 

48 

49 

50class TransactionClosedError(DatabaseRuntimeError): 

51 """Raised when a Transaction is used after it has closed.""" 

52 

53 

54class PoolTimeoutError(DatabaseRuntimeError): 

55 """Raised when acquiring a database connection exceeds the timeout.""" 

56 

57 

58class DatabaseCloseTimeoutError(DatabaseRuntimeError): 

59 """Raised when Database.close cannot finish before its timeout.""" 

60 

61 

62class DatabaseClosingError(DatabaseRuntimeError): 

63 """Raised when new work starts while Database.close is in progress.""" 

64 

65 

66class ExecutionError(DatabaseRuntimeError): 

67 """Database execution failure with query context. 

68 

69 >>> error = ExecutionError("failed", sql="SELECT ?", params=(1,)) 

70 >>> error.sql 

71 'SELECT ?' 

72 """ 

73 

74 sql: str 

75 params: tuple[object, ...] 

76 

77 def __init__( 

78 self, 

79 message: str, 

80 *, 

81 sql: str, 

82 params: tuple[object, ...], 

83 ) -> None: 

84 super().__init__(message) 

85 self.sql: str = sql 

86 self.params: tuple[object, ...] = params 

87 

88 def __str__(self) -> str: 

89 message = super().__str__() 

90 return f"{message} sql={self.sql!r} params={self.params!r}" 

91 

92 

93class SchemaError(SnekqlError): 

94 """Base class for schema creation and verification failures.""" 

95 

96 

97class SchemaVerificationError(SchemaError): 

98 """Raised when an existing database table drifts from model DDL."""