claude_code_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
54#[derive(Debug, Error)]
56#[error("CLI not found: {message}")]
57pub struct CliNotFoundError {
58 pub message: String,
60 pub cli_path: Option<PathBuf>,
62}
63
64impl CliNotFoundError {
65 pub fn new(message: impl Into<String>, cli_path: Option<PathBuf>) -> Self {
67 Self {
68 message: message.into(),
69 cli_path,
70 }
71 }
72}
73
74#[derive(Debug, Error)]
76#[error("Connection error: {message}")]
77pub struct ConnectionError {
78 pub message: String,
80}
81
82impl ConnectionError {
83 pub fn new(message: impl Into<String>) -> Self {
85 Self {
86 message: message.into(),
87 }
88 }
89}
90
91#[derive(Debug, Error)]
93#[error("Process error (exit code {exit_code:?}): {message}")]
94pub struct ProcessError {
95 pub message: String,
97 pub exit_code: Option<i32>,
99 pub stderr: Option<String>,
101}
102
103impl ProcessError {
104 pub fn new(message: impl Into<String>, exit_code: Option<i32>, stderr: Option<String>) -> Self {
106 Self {
107 message: message.into(),
108 exit_code,
109 stderr,
110 }
111 }
112}
113
114#[derive(Debug, Error)]
116#[error("JSON decode error: {message}")]
117pub struct JsonDecodeError {
118 pub message: String,
120 pub line: String,
122}
123
124impl JsonDecodeError {
125 pub fn new(message: impl Into<String>, line: impl Into<String>) -> Self {
127 Self {
128 message: message.into(),
129 line: line.into(),
130 }
131 }
132}
133
134#[derive(Debug, Error)]
136#[error("Message parse error: {message}")]
137pub struct MessageParseError {
138 pub message: String,
140 pub data: Option<serde_json::Value>,
142}
143
144impl MessageParseError {
145 pub fn new(message: impl Into<String>, data: Option<serde_json::Value>) -> Self {
147 Self {
148 message: message.into(),
149 data,
150 }
151 }
152}
153
154#[derive(Debug, Error)]
156#[error("Image validation error: {message}")]
157pub struct ImageValidationError {
158 pub message: String,
160}
161
162impl ImageValidationError {
163 pub fn new(message: impl Into<String>) -> Self {
165 Self {
166 message: message.into(),
167 }
168 }
169}
170
171pub type Result<T> = std::result::Result<T, ClaudeError>;