floxide_core/
error.rs

1use std::fmt::Display;
2use thiserror::Error;
3
4/// All possible errors that can occur in the floxide framework
5#[derive(Error, Debug, Clone)]
6pub enum FloxideError {
7    /// Error related to node execution
8    #[error("Node execution error: {0}")]
9    NodeExecution(String),
10
11    /// Error related to workflow execution
12    #[error("Workflow execution error: {0}")]
13    WorkflowExecution(String),
14
15    /// Error when a node is not found in the workflow
16    #[error("Node not found: {0}")]
17    NodeNotFound(String),
18
19    /// Error when an action transition is not defined
20    #[error("No transition defined for action: {0}")]
21    NoTransitionDefined(String),
22
23    /// Error during serialization
24    #[error("Serialization error: {0}")]
25    SerializationError(String),
26
27    /// Error during deserialization
28    #[error("Deserialization error: {0}")]
29    DeserializationError(String),
30
31    /// Error during async task joining
32    #[error("Task join error: {0}")]
33    JoinError(String),
34
35    /// A cycle was detected in the workflow
36    #[error("Cycle detected in workflow execution")]
37    WorkflowCycleDetected,
38
39    /// Error in the workflow definition
40    #[error("Workflow definition error: {0}")]
41    WorkflowDefinitionError(String),
42
43    /// Error during batch processing
44    #[error("Batch processing error: {0}")]
45    BatchProcessingError(String),
46
47    /// Node returned an unexpected outcome
48    #[error("Unexpected node outcome: {0}")]
49    UnexpectedOutcome(String),
50
51    /// Other generic errors
52    #[error("{0}")]
53    Other(String),
54}
55
56/// A specialized Result type for floxide operations
57pub type FloxideResult<T> = Result<T, FloxideError>;
58
59impl FloxideError {
60    /// Create a new node execution error
61    pub fn node_execution(node_id: impl Display, message: impl Display) -> Self {
62        Self::NodeExecution(format!("Node {}: {}", node_id, message))
63    }
64
65    /// Create a new batch processing error
66    pub fn batch_processing(
67        message: impl Display,
68        source: Box<dyn std::error::Error + Send + Sync>,
69    ) -> Self {
70        Self::BatchProcessingError(format!("{}: {}", message, source))
71    }
72
73    /// Create a new unexpected outcome error
74    pub fn unexpected_outcome(message: impl Display) -> Self {
75        Self::UnexpectedOutcome(message.to_string())
76    }
77
78    /// Create a new node not found error
79    pub fn node_not_found(node_id: impl Display) -> Self {
80        Self::NodeNotFound(node_id.to_string())
81    }
82
83    /// Create a new timeout error
84    pub fn timeout(message: impl Display) -> Self {
85        Self::Other(format!("Timeout: {}", message))
86    }
87
88    /// Check if this error is a timeout error
89    pub fn is_timeout(&self) -> bool {
90        match self {
91            Self::Other(msg) => msg.starts_with("Timeout:"),
92            _ => false,
93        }
94    }
95}