claude_agent_sdk/
errors.rs

1//! Error types for the Claude Agent SDK
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Main error type for the Claude Agent SDK
7#[derive(Debug, Error)]
8pub enum ClaudeError {
9    /// CLI connection error
10    #[error("CLI connection error: {0}")]
11    Connection(#[from] ConnectionError),
12
13    /// Process error
14    #[error("Process error: {0}")]
15    Process(#[from] ProcessError),
16
17    /// JSON decode error
18    #[error("JSON decode error: {0}")]
19    JsonDecode(#[from] JsonDecodeError),
20
21    /// Message parse error
22    #[error("Message parse error: {0}")]
23    MessageParse(#[from] MessageParseError),
24
25    /// Transport error
26    #[error("Transport error: {0}")]
27    Transport(String),
28
29    /// Control protocol error
30    #[error("Control protocol error: {0}")]
31    ControlProtocol(String),
32
33    /// Invalid configuration
34    #[error("Invalid configuration: {0}")]
35    InvalidConfig(String),
36
37    /// CLI not found error
38    #[error("CLI not found: {0}")]
39    CliNotFound(#[from] CliNotFoundError),
40
41    /// Image validation error
42    #[error("Image validation error: {0}")]
43    ImageValidation(#[from] ImageValidationError),
44
45    /// IO error
46    #[error("IO error: {0}")]
47    Io(#[from] std::io::Error),
48
49    /// Other errors
50    #[error(transparent)]
51    Other(#[from] anyhow::Error),
52
53    /// Not found error
54    #[error("Not found: {0}")]
55    NotFound(String),
56
57    /// Invalid input error
58    #[error("Invalid input: {0}")]
59    InvalidInput(String),
60
61    /// Internal error
62    #[error("Internal error: {0}")]
63    InternalError(String),
64}
65
66/// Error when Claude Code CLI cannot be found
67#[derive(Debug, Error)]
68#[error("CLI not found: {message}")]
69pub struct CliNotFoundError {
70    /// Error message
71    pub message: String,
72    /// Path that was checked
73    pub cli_path: Option<PathBuf>,
74}
75
76impl CliNotFoundError {
77    /// Create a new CLI not found error
78    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/// Error when connecting to Claude Code CLI
87#[derive(Debug, Error)]
88#[error("Connection error: {message}")]
89pub struct ConnectionError {
90    /// Error message
91    pub message: String,
92}
93
94impl ConnectionError {
95    /// Create a new connection error
96    pub fn new(message: impl Into<String>) -> Self {
97        Self {
98            message: message.into(),
99        }
100    }
101}
102
103/// Error when the CLI process fails
104#[derive(Debug, Error)]
105#[error("Process error (exit code {exit_code:?}): {message}")]
106pub struct ProcessError {
107    /// Error message
108    pub message: String,
109    /// Process exit code
110    pub exit_code: Option<i32>,
111    /// stderr output
112    pub stderr: Option<String>,
113}
114
115impl ProcessError {
116    /// Create a new process error
117    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/// Error when JSON decoding fails
127#[derive(Debug, Error)]
128#[error("JSON decode error: {message}")]
129pub struct JsonDecodeError {
130    /// Error message
131    pub message: String,
132    /// The line that failed to decode
133    pub line: String,
134}
135
136impl JsonDecodeError {
137    /// Create a new JSON decode error
138    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/// Error when message parsing fails
147#[derive(Debug, Error)]
148#[error("Message parse error: {message}")]
149pub struct MessageParseError {
150    /// Error message
151    pub message: String,
152    /// The data that failed to parse
153    pub data: Option<serde_json::Value>,
154}
155
156impl MessageParseError {
157    /// Create a new message parse error
158    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/// Image validation error
167#[derive(Debug, Error)]
168#[error("Image validation error: {message}")]
169pub struct ImageValidationError {
170    /// Error message
171    pub message: String,
172}
173
174impl ImageValidationError {
175    /// Create a new image validation error
176    pub fn new(message: impl Into<String>) -> Self {
177        Self {
178            message: message.into(),
179        }
180    }
181}
182
183/// Result type for the Claude Agent SDK
184pub type Result<T> = std::result::Result<T, ClaudeError>;