actflow/
error.rs

1//! Error types for Actflow.
2//!
3//! All errors in Actflow are represented by the `ActflowError` enum,
4//! which provides specific variants for different error categories.
5
6use std::{io::ErrorKind, string::FromUtf8Error};
7
8use serde::{Deserialize, Serialize};
9use thiserror::Error;
10
11/// Unified error type for all Actflow operations.
12///
13/// Each variant represents a specific category of error that can occur
14/// during workflow definition, execution, or storage operations.
15#[derive(Deserialize, Serialize, Error, Debug, Clone, PartialEq)]
16pub enum ActflowError {
17    /// Engine-level errors (startup, shutdown, configuration).
18    #[error("{0}")]
19    Engine(String),
20
21    /// Configuration parsing or validation errors.
22    #[error("{0}")]
23    Config(String),
24
25    /// Data conversion errors (JSON, protobuf, etc.).
26    #[error("{0}")]
27    Convert(String),
28
29    /// Script execution errors (JavaScript, Python).
30    #[error("{0}")]
31    Script(String),
32
33    /// Structured exception with error code.
34    #[error("ecode: {ecode}, message: {message}")]
35    Exception {
36        ecode: String,
37        message: String,
38    },
39
40    /// Runtime execution errors.
41    #[error("{0}")]
42    Runtime(String),
43
44    /// Storage operation errors.
45    #[error("{0}")]
46    Store(String),
47
48    /// Process lifecycle errors.
49    #[error("{0}")]
50    Process(String),
51
52    /// Workflow definition errors.
53    #[error("{0}")]
54    Workflow(String),
55
56    /// Node definition or execution errors.
57    #[error("{0}")]
58    Node(String),
59
60    /// Edge definition errors.
61    #[error("{0}")]
62    Edge(String),
63
64    /// Action execution errors.
65    #[error("{0}")]
66    Action(String),
67
68    /// I/O operation errors.
69    #[error("{0}")]
70    IoError(String),
71
72    /// Message queue errors.
73    #[error("{0}")]
74    Queue(String),
75}
76
77impl From<ActflowError> for String {
78    fn from(val: ActflowError) -> Self {
79        val.to_string()
80    }
81}
82
83impl From<std::io::Error> for ActflowError {
84    fn from(error: std::io::Error) -> Self {
85        ActflowError::IoError(error.to_string())
86    }
87}
88
89impl From<ActflowError> for std::io::Error {
90    fn from(val: ActflowError) -> Self {
91        #[allow(clippy::io_other_error)]
92        std::io::Error::new(ErrorKind::Other, val.to_string())
93    }
94}
95
96impl From<FromUtf8Error> for ActflowError {
97    fn from(_: FromUtf8Error) -> Self {
98        ActflowError::Runtime("Error with utf-8 string convert".to_string())
99    }
100}
101
102impl From<serde_json::Error> for ActflowError {
103    fn from(error: serde_json::Error) -> Self {
104        ActflowError::Convert(error.to_string())
105    }
106}
107
108impl From<jsonschema::ValidationError<'_>> for ActflowError {
109    fn from(error: jsonschema::ValidationError<'_>) -> Self {
110        ActflowError::Runtime(error.to_string())
111    }
112}