Skip to main content

lattice_ai/mcp/
error.rs

1//! Error type for the Claude Code IDE peer.
2//!
3//! Per the design's graceful-degradation contract (ยง5): every WS-thread
4//! failure logs-and-skips; a dropped host receiver yields a JSON-RPC
5//! error to the agent, never a hang or panic. This enum is the recoverable
6//! error currency on the connection-serving path.
7
8use thiserror::Error;
9
10/// Errors raised while serving an agent connection or managing the server.
11#[derive(Debug, Error)]
12pub enum ClaudeCodeError {
13    /// Socket / lockfile I/O failure.
14    #[error("io error: {0}")]
15    Io(#[from] std::io::Error),
16
17    /// WebSocket transport / handshake failure (including auth rejection
18    /// surfaced by the handshake). Boxed because `tungstenite::Error` is a
19    /// ~136-byte enum; boxing keeps `ClaudeCodeError` small so every
20    /// `Result<_, ClaudeCodeError>` stays cheap to move (clippy
21    /// `result_large_err`). The manual `From` below preserves `?`
22    /// ergonomics on a bare `tungstenite::Error`.
23    #[error("websocket error: {0}")]
24    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
25
26    /// JSON (de)serialization failure on the wire.
27    #[error("json error: {0}")]
28    Json(#[from] serde_json::Error),
29
30    /// The OS random source was unavailable when minting an auth token.
31    #[error("random source unavailable: {0}")]
32    Random(String),
33}
34
35impl From<tokio_tungstenite::tungstenite::Error> for ClaudeCodeError {
36    fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
37        ClaudeCodeError::WebSocket(Box::new(e))
38    }
39}
40
41/// Crate-local result alias.
42pub type Result<T> = std::result::Result<T, ClaudeCodeError>;