Skip to main content

excel_mcp_server/
error.rs

1use std::fmt;
2
3/// Central error type for the Excel MCP server.
4///
5/// Each variant carries a descriptive `String` message so that callers
6/// (ultimately the LLM client) receive actionable feedback.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum ExcelMcpError {
9    /// The requested resource (workbook, sheet, cell, etc.) was not found.
10    NotFound(String),
11    /// The caller supplied an invalid parameter value.
12    InvalidInput(String),
13    /// The requested operation is not supported by the engine that owns the workbook.
14    EngineUnsupported(String),
15    /// The workbook store has reached its maximum capacity.
16    CapacityExceeded(String),
17    /// An I/O error occurred (file read/write, path issues, etc.).
18    IoError(String),
19    /// A parsing error occurred (cell references, data formats, etc.).
20    ParseError(String),
21    /// The workbook was evicted from the store due to inactivity.
22    Evicted(String),
23}
24
25impl fmt::Display for ExcelMcpError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            ExcelMcpError::NotFound(msg) => write!(f, "Not found: {msg}"),
29            ExcelMcpError::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
30            ExcelMcpError::EngineUnsupported(msg) => write!(f, "Engine unsupported: {msg}"),
31            ExcelMcpError::CapacityExceeded(msg) => write!(f, "Capacity exceeded: {msg}"),
32            ExcelMcpError::IoError(msg) => write!(f, "IO error: {msg}"),
33            ExcelMcpError::ParseError(msg) => write!(f, "Parse error: {msg}"),
34            ExcelMcpError::Evicted(msg) => write!(f, "Evicted: {msg}"),
35        }
36    }
37}
38
39impl std::error::Error for ExcelMcpError {}
40
41impl From<std::io::Error> for ExcelMcpError {
42    fn from(err: std::io::Error) -> Self {
43        ExcelMcpError::IoError(err.to_string())
44    }
45}
46
47impl From<serde_json::Error> for ExcelMcpError {
48    fn from(err: serde_json::Error) -> Self {
49        ExcelMcpError::ParseError(err.to_string())
50    }
51}