Skip to main content

robin_sparkless/
error.rs

1//! Engine error type for embedders.
2//!
3//! Use [`EngineError`] when you want to map robin-sparkless and Polars errors
4//! to a single type (e.g. for FFI or CLI) without depending on Polars error types.
5
6use polars::error::PolarsError;
7use std::fmt;
8
9/// Unified error type for robin-sparkless operations.
10///
11/// Embedders (Python, Node, CLI) can map these variants to native errors
12/// without depending on `PolarsError`.
13#[derive(Debug)]
14pub enum EngineError {
15    /// User-facing error (invalid input, unsupported operation).
16    User(String),
17    /// Internal / compute error.
18    Internal(String),
19    /// I/O error (file not found, permission, etc.).
20    Io(String),
21    /// SQL parsing or execution error.
22    Sql(String),
23    /// Resource not found (column, table, file).
24    NotFound(String),
25    /// Other / unclassified.
26    Other(String),
27}
28
29impl fmt::Display for EngineError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            EngineError::User(s) => write!(f, "user error: {s}"),
33            EngineError::Internal(s) => write!(f, "internal error: {s}"),
34            EngineError::Io(s) => write!(f, "io error: {s}"),
35            EngineError::Sql(s) => write!(f, "sql error: {s}"),
36            EngineError::NotFound(s) => write!(f, "not found: {s}"),
37            EngineError::Other(s) => write!(f, "{s}"),
38        }
39    }
40}
41
42impl std::error::Error for EngineError {}
43
44impl From<PolarsError> for EngineError {
45    fn from(e: PolarsError) -> Self {
46        let msg = e.to_string();
47        match &e {
48            PolarsError::ColumnNotFound(_) => EngineError::NotFound(msg),
49            PolarsError::InvalidOperation(_) => EngineError::User(msg),
50            PolarsError::ComputeError(_) => EngineError::Internal(msg),
51            PolarsError::IO { .. } => EngineError::Io(msg),
52            _ => EngineError::Other(msg),
53        }
54    }
55}
56
57impl From<serde_json::Error> for EngineError {
58    fn from(e: serde_json::Error) -> Self {
59        EngineError::Internal(e.to_string())
60    }
61}
62
63impl From<std::io::Error> for EngineError {
64    fn from(e: std::io::Error) -> Self {
65        EngineError::Io(e.to_string())
66    }
67}