claude_agent_sdk/
error.rs1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum ClaudeError {
8 #[error("Claude Code CLI not found: {0}")]
10 CliNotFound(String),
11
12 #[error("Connection error: {0}")]
14 Connection(String),
15
16 #[error("Process error (exit code {exit_code}): {message}")]
18 Process {
19 message: String,
21 exit_code: i32,
23 stderr: Option<String>,
25 },
26
27 #[error("JSON decode error: {0}")]
29 JsonDecode(#[from] serde_json::Error),
30
31 #[error("Message parse error: {message}")]
33 MessageParse {
34 message: String,
36 data: Option<serde_json::Value>,
38 },
39
40 #[error("Transport error: {0}")]
42 Transport(String),
43
44 #[error("Control protocol error: {0}")]
46 ControlProtocol(String),
47
48 #[error("Hook error: {0}")]
50 Hook(String),
51
52 #[error("MCP error: {0}")]
54 Mcp(String),
55
56 #[error("IO error: {0}")]
58 Io(#[from] std::io::Error),
59
60 #[error("Timeout: {0}")]
62 Timeout(String),
63
64 #[error("Invalid configuration: {0}")]
66 InvalidConfig(String),
67}
68
69pub type Result<T> = std::result::Result<T, ClaudeError>;
71
72impl ClaudeError {
73 pub fn cli_not_found() -> Self {
75 Self::CliNotFound(
76 "Claude Code not found. Install with:\n\
77 npm install -g @anthropic-ai/claude-code\n\
78 \n\
79 If already installed locally, try:\n\
80 export PATH=\"$HOME/node_modules/.bin:$PATH\"\n\
81 \n\
82 Or specify the path when creating transport"
83 .to_string(),
84 )
85 }
86
87 pub fn connection(msg: impl Into<String>) -> Self {
89 Self::Connection(msg.into())
90 }
91
92 pub fn process(msg: impl Into<String>, exit_code: i32, stderr: Option<String>) -> Self {
94 Self::Process {
95 message: msg.into(),
96 exit_code,
97 stderr,
98 }
99 }
100
101 pub fn message_parse(msg: impl Into<String>, data: Option<serde_json::Value>) -> Self {
103 Self::MessageParse {
104 message: msg.into(),
105 data,
106 }
107 }
108
109 pub fn transport(msg: impl Into<String>) -> Self {
111 Self::Transport(msg.into())
112 }
113
114 pub fn control_protocol(msg: impl Into<String>) -> Self {
116 Self::ControlProtocol(msg.into())
117 }
118
119 pub fn protocol_error(msg: impl Into<String>) -> Self {
121 Self::ControlProtocol(msg.into())
122 }
123
124 pub fn json_encode(msg: impl Into<String>) -> Self {
126 Self::JsonDecode(serde_json::Error::io(std::io::Error::new(
127 std::io::ErrorKind::InvalidData,
128 msg.into(),
129 )))
130 }
131
132 pub fn json_decode(msg: impl Into<String>) -> Self {
134 Self::JsonDecode(serde_json::Error::io(std::io::Error::new(
135 std::io::ErrorKind::InvalidData,
136 msg.into(),
137 )))
138 }
139
140 pub fn hook(msg: impl Into<String>) -> Self {
142 Self::Hook(msg.into())
143 }
144
145 pub fn mcp(msg: impl Into<String>) -> Self {
147 Self::Mcp(msg.into())
148 }
149
150 pub fn timeout(msg: impl Into<String>) -> Self {
152 Self::Timeout(msg.into())
153 }
154
155 pub fn invalid_config(msg: impl Into<String>) -> Self {
157 Self::InvalidConfig(msg.into())
158 }
159}