claude_agent_sdk/
errors.rs1use std::path::PathBuf;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum ClaudeError {
9 #[error("CLI connection error: {0}")]
11 Connection(#[from] ConnectionError),
12
13 #[error("Process error: {0}")]
15 Process(#[from] ProcessError),
16
17 #[error("JSON decode error: {0}")]
19 JsonDecode(#[from] JsonDecodeError),
20
21 #[error("Message parse error: {0}")]
23 MessageParse(#[from] MessageParseError),
24
25 #[error("Transport error: {0}")]
27 Transport(String),
28
29 #[error("Control protocol error: {0}")]
31 ControlProtocol(String),
32
33 #[error("Invalid configuration: {0}")]
35 InvalidConfig(String),
36
37 #[error("CLI not found: {0}")]
39 CliNotFound(#[from] CliNotFoundError),
40
41 #[error("Image validation error: {0}")]
43 ImageValidation(#[from] ImageValidationError),
44
45 #[error("IO error: {0}")]
47 Io(#[from] std::io::Error),
48
49 #[error(transparent)]
51 Other(#[from] anyhow::Error),
52
53 #[error("Not found: {0}")]
55 NotFound(String),
56
57 #[error("Invalid input: {0}")]
59 InvalidInput(String),
60
61 #[error("Internal error: {0}")]
63 InternalError(String),
64}
65
66#[derive(Debug, Error)]
68#[error("CLI not found: {message}")]
69pub struct CliNotFoundError {
70 pub message: String,
72 pub cli_path: Option<PathBuf>,
74}
75
76impl CliNotFoundError {
77 pub fn new(message: impl Into<String>, cli_path: Option<PathBuf>) -> Self {
79 Self {
80 message: message.into(),
81 cli_path,
82 }
83 }
84}
85
86#[derive(Debug, Error)]
88#[error("Connection error: {message}")]
89pub struct ConnectionError {
90 pub message: String,
92}
93
94impl ConnectionError {
95 pub fn new(message: impl Into<String>) -> Self {
97 Self {
98 message: message.into(),
99 }
100 }
101}
102
103#[derive(Debug, Error)]
105#[error("Process error (exit code {exit_code:?}): {message}")]
106pub struct ProcessError {
107 pub message: String,
109 pub exit_code: Option<i32>,
111 pub stderr: Option<String>,
113}
114
115impl ProcessError {
116 pub fn new(message: impl Into<String>, exit_code: Option<i32>, stderr: Option<String>) -> Self {
118 Self {
119 message: message.into(),
120 exit_code,
121 stderr,
122 }
123 }
124}
125
126#[derive(Debug, Error)]
128#[error("JSON decode error: {message}")]
129pub struct JsonDecodeError {
130 pub message: String,
132 pub line: String,
134}
135
136impl JsonDecodeError {
137 pub fn new(message: impl Into<String>, line: impl Into<String>) -> Self {
139 Self {
140 message: message.into(),
141 line: line.into(),
142 }
143 }
144}
145
146#[derive(Debug, Error)]
148#[error("Message parse error: {message}")]
149pub struct MessageParseError {
150 pub message: String,
152 pub data: Option<serde_json::Value>,
154}
155
156impl MessageParseError {
157 pub fn new(message: impl Into<String>, data: Option<serde_json::Value>) -> Self {
159 Self {
160 message: message.into(),
161 data,
162 }
163 }
164}
165
166#[derive(Debug, Error)]
168#[error("Image validation error: {message}")]
169pub struct ImageValidationError {
170 pub message: String,
172}
173
174impl ImageValidationError {
175 pub fn new(message: impl Into<String>) -> Self {
177 Self {
178 message: message.into(),
179 }
180 }
181}
182
183pub type Result<T> = std::result::Result<T, ClaudeError>;