Coverage for src/dataknobs_fsm/core/exceptions.py: 56%

1"""FSM Core Exceptions Module.

2

3 This module defines core exception types used throughout the FSM system.

4"""

5

6 from typing import Any, Dict

7

8

9 class FSMError(Exception):

10 """Base exception for all FSM-related errors."""

11

12 def __init__(self, message: str, details: Dict[str, Any] | None = None):

13 super().__init__(message)

14 self.details = details or {}

15

16

17 class InvalidConfigurationError(FSMError):

18 """Raised when FSM configuration is invalid."""

19 pass

20

21

22 class StateExecutionError(FSMError):

23 """Raised when state execution fails."""

24

25 def __init__(self, state_name: str, message: str, details: Dict[str, Any] | None = None):

26 super().__init__(f"State '{state_name}' execution failed: {message}", details)

27 self.state_name = state_name

28

29

30 class TransitionError(FSMError):

31 """Raised when state transition fails."""

32

33 def __init__(self, from_state: str, to_state: str, message: str, details: Dict[str, Any] | None = None):

34 super().__init__(f"Transition from '{from_state}' to '{to_state}' failed: {message}", details)

35 self.from_state = from_state

36 self.to_state = to_state

37

38

39 class ResourceError(FSMError):

40 """Raised when resource operations fail."""

41

42 def __init__(self, resource_id: str, message: str, details: Dict[str, Any] | None = None):

43 super().__init__(f"Resource '{resource_id}' error: {message}", details)

44 self.resource_id = resource_id

45

46

47 class ValidationError(FSMError):

48 """Raised when data validation fails."""

49 pass

50

51

52 class FunctionError(FSMError):

53 """Raised when a function execution fails.

54

55 This represents deterministic failures in user-defined functions

56 (transforms, validators, conditions) that should not be retried.

57 These are code errors, not transient failures.

58 """

59

60 def __init__(self,

61 message: str,

62 function_name: str | None = None,

63 from_state: str | None = None,

64 to_state: str | None = None,

65 details: Dict[str, Any] | None = None):

66 if function_name:

67 message = f"Function '{function_name}' failed: {message}"

68 super().__init__(message, details)

69 self.function_name = function_name

70 self.from_state = from_state

71 self.to_state = to_state

72

73

74 class TimeoutError(FSMError):

75 """Raised when operation times out."""

76 pass

77

78

79 class ConcurrencyError(FSMError):

80 """Raised when concurrent execution fails."""

81 pass

82

83

84 class CircuitBreakerError(FSMError):

85 """Raised when circuit breaker is open."""

86

87 def __init__(self, wait_time: float | None = None, details: Dict[str, Any] | None = None):

88 if wait_time:

89 message = f"Circuit breaker is open (wait {wait_time:.1f}s)"

90 else:

91 message = "Circuit breaker is open"

92 super().__init__(message, details)

93 self.wait_time = wait_time

94

95

96 class ETLError(FSMError):

97 """Raised when ETL operations fail."""

98 pass

99

100

101 class BulkheadTimeoutError(FSMError):

102 """Raised when bulkhead queue times out."""

103 pass