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    // E9xx: Internal Errors
24    InternalError(String),
25    /// Arbitrary error with custom code & message (mainly for tests)
26    Custom { code: String, msg: String },
27}
28
29impl FCError {
30    /// Creates a new custom FCError with an arbitrary error code string (e.g. "E001").
31    pub fn new(code: &str, msg: String) -> Self {
32        FCError::Custom { code: code.to_string(), msg }
33    }
34}
35
36impl fmt::Display for FCError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            FCError::UnknownCommand(msg) => write!(f, "[E100] Unknown command: {}", msg),
40            FCError::Suggestion(cmd, sugg) => write!(f, "[E101] Did you mean '{}' instead of '{}'?", sugg, cmd),
41            FCError::ParseError(msg) => write!(f, "[E102] Parse error: {}", msg),
42            FCError::InsufficientData(msg) => write!(f, "[E200] Insufficient data: {}", msg),
43            FCError::InvalidArgument(msg) => write!(f, "[E201] Invalid argument: {}", msg),
44            FCError::TypeMismatch { expected, found, context } => write!(f, "[E300] Type mismatch in {}: expected '{}' but found '{}'", context, expected, found),
45            FCError::TypeCoercionFailed { from, to, value } => write!(f, "[E301] Type coercion failed: cannot convert '{}' from '{}' to '{}'", value, from, to),
46            FCError::UnsupportedType(msg) => write!(f, "[E302] Unsupported type: {}", msg),
47            FCError::InternalError(msg) => write!(f, "[E900] Internal error: {}", msg),
48            FCError::Custom { code, msg } => write!(f, "[{}] {}", code, msg),
49        }
50    }
51}
52
53// Optional: Implement std::error::Error for better interoperability if needed later.
54// For now, fmt::Display and Debug are sufficient as per the plan.
55// impl std::error::Error for FCError {}