Skip to main content

claude_code_rs/
error.rs

1use std::io;
2
3/// All errors that can occur in the Claude Agent SDK.
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6    #[error("claude CLI not found in PATH")]
7    CliNotFound,
8
9    #[error("claude CLI version {found} too old, need >= {required}")]
10    CliVersionTooOld { found: String, required: String },
11
12    #[error("failed to connect to CLI process: {0}")]
13    CliConnection(String),
14
15    #[error("CLI process error: {0}")]
16    Process(String),
17
18    #[error("CLI process exited with code {code}: {stderr}")]
19    ProcessExit { code: i32, stderr: String },
20
21    #[error("JSON decode error: {0}")]
22    JsonDecode(#[from] serde_json::Error),
23
24    #[error("failed to parse message: {reason}")]
25    MessageParse { reason: String },
26
27    #[error("control protocol timeout after {0:?}")]
28    ControlTimeout(std::time::Duration),
29
30    #[error("control protocol error: {0}")]
31    ControlProtocol(String),
32
33    #[error("transport closed")]
34    TransportClosed,
35
36    #[error("not connected")]
37    NotConnected,
38
39    #[error("already connected")]
40    AlreadyConnected,
41
42    #[error("IO error: {0}")]
43    Io(#[from] io::Error),
44
45    #[error("MCP error: {code}: {message}")]
46    Mcp { code: i64, message: String },
47
48    #[error("hook error: {0}")]
49    Hook(String),
50}
51
52pub type Result<T> = std::result::Result<T, Error>;