flowcode_core/
error.rs

1// flowcode-core/src/error.rs
2
3use std::fmt;
4use crate::types::ValueKind;
5
6/// Represents a FlowCode error with a specific code and message.
7#[derive(Debug, PartialEq)]
8pub enum FCError {
9    // E1xx: Input / Parse Errors
10    UnknownCommand(String),
11    Suggestion(String, String), // command, suggestion
12    ParseError(String),
13    
14    // E2xx: Execution / Runtime Errors
15    InsufficientData(String), // reason
16    InvalidArgument(String),  // reason
17    
18    // E3xx: Type System Errors (Added for v0.4-alpha)
19    TypeMismatch { expected: String, found: String, context: String },
20    TypeCoercionFailed { from: ValueKind, to: ValueKind, value: String },
21    UnsupportedType(String),
22    
23    // E4xx: Backend Errors (Added for backend integration)
24    BackendError(String),
25    // DuckDB-specific errors
26    DuckDBConnectionError(String),
27    DuckDBQueryError(String),
28    DuckDBDataConversionError(String),
29    
30    // E9xx: Internal Errors
31    InternalError(String),
32    /// Arbitrary error with custom code & message (mainly for tests)
33    Custom { code: String, msg: String },
34}
35
36impl FCError {
37    /// Creates a new custom FCError with an arbitrary error code string (e.g. "E001").
38    pub fn new(code: &str, msg: String) -> Self {
39        FCError::Custom { code: code.to_string(), msg }
40    }
41}
42
43impl fmt::Display for FCError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            FCError::UnknownCommand(msg) => write!(f, "[E100] Unknown command: {}", msg),
47            FCError::Suggestion(cmd, sugg) => write!(f, "[E101] Did you mean '{}' instead of '{}'?", sugg, cmd),
48            FCError::ParseError(msg) => write!(f, "[E102] Parse error: {}", msg),
49            FCError::InsufficientData(msg) => write!(f, "[E200] Insufficient data: {}", msg),
50            FCError::InvalidArgument(msg) => write!(f, "[E201] Invalid argument: {}", msg),
51            FCError::TypeMismatch { expected, found, context } => write!(f, "[E300] Type mismatch in {}: expected '{}' but found '{}'", context, expected, found),
52            FCError::TypeCoercionFailed { from, to, value } => write!(f, "[E301] Type coercion failed: cannot convert '{}' from '{}' to '{}'", value, from, to),
53            FCError::UnsupportedType(msg) => write!(f, "[E302] Unsupported type: {}", msg),
54            FCError::BackendError(msg) => write!(f, "[E400] Backend error: {}", msg),
55            FCError::DuckDBConnectionError(msg) => write!(f, "[E401] DuckDB connection error: {}", msg),
56            FCError::DuckDBQueryError(msg) => write!(f, "[E402] DuckDB query error: {}", msg),
57            FCError::DuckDBDataConversionError(msg) => write!(f, "[E403] DuckDB data conversion error: {}", msg),
58            FCError::InternalError(msg) => write!(f, "[E900] Internal error: {}", msg),
59            FCError::Custom { code, msg } => write!(f, "[{}] {}", code, msg),
60        }
61    }
62}
63
64impl std::error::Error for FCError {}