flowcode-core 0.4.3-alpha

Core execution engine for FlowCode data scripting language
Documentation
// flowcode-core/src/error.rs

use std::fmt;
use crate::types::ValueKind;

/// Represents a FlowCode error with a specific code and message.
#[derive(Debug, PartialEq)]
pub enum FCError {
    // E1xx: Input / Parse Errors
    UnknownCommand(String),
    Suggestion(String, String), // command, suggestion
    ParseError(String),
    
    // E2xx: Execution / Runtime Errors
    InsufficientData(String), // reason
    InvalidArgument(String),  // reason
    
    // E3xx: Type System Errors (Added for v0.4-alpha)
    TypeMismatch { expected: String, found: String, context: String },
    TypeCoercionFailed { from: ValueKind, to: ValueKind, value: String },
    UnsupportedType(String),
    
    // E4xx: Backend Errors (Added for backend integration)
    BackendError(String),
    // DuckDB-specific errors
    DuckDBConnectionError(String),
    DuckDBQueryError(String),
    DuckDBDataConversionError(String),
    
    // E9xx: Internal Errors
    InternalError(String),
    /// Arbitrary error with custom code & message (mainly for tests)
    Custom { code: String, msg: String },
}

impl FCError {
    /// Creates a new custom FCError with an arbitrary error code string (e.g. "E001").
    pub fn new(code: &str, msg: String) -> Self {
        FCError::Custom { code: code.to_string(), msg }
    }
}

impl fmt::Display for FCError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FCError::UnknownCommand(msg) => write!(f, "[E100] Unknown command: {}", msg),
            FCError::Suggestion(cmd, sugg) => write!(f, "[E101] Did you mean '{}' instead of '{}'?", sugg, cmd),
            FCError::ParseError(msg) => write!(f, "[E102] Parse error: {}", msg),
            FCError::InsufficientData(msg) => write!(f, "[E200] Insufficient data: {}", msg),
            FCError::InvalidArgument(msg) => write!(f, "[E201] Invalid argument: {}", msg),
            FCError::TypeMismatch { expected, found, context } => write!(f, "[E300] Type mismatch in {}: expected '{}' but found '{}'", context, expected, found),
            FCError::TypeCoercionFailed { from, to, value } => write!(f, "[E301] Type coercion failed: cannot convert '{}' from '{}' to '{}'", value, from, to),
            FCError::UnsupportedType(msg) => write!(f, "[E302] Unsupported type: {}", msg),
            FCError::BackendError(msg) => write!(f, "[E400] Backend error: {}", msg),
            FCError::DuckDBConnectionError(msg) => write!(f, "[E401] DuckDB connection error: {}", msg),
            FCError::DuckDBQueryError(msg) => write!(f, "[E402] DuckDB query error: {}", msg),
            FCError::DuckDBDataConversionError(msg) => write!(f, "[E403] DuckDB data conversion error: {}", msg),
            FCError::InternalError(msg) => write!(f, "[E900] Internal error: {}", msg),
            FCError::Custom { code, msg } => write!(f, "[{}] {}", code, msg),
        }
    }
}

impl std::error::Error for FCError {}