claude_code_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
54/// Error when Claude Code CLI cannot be found
55#[derive(Debug, Error)]
56#[error("CLI not found: {message}")]
57pub struct CliNotFoundError {
58    /// Error message
59    pub message: String,
60    /// Path that was checked
61    pub cli_path: Option<PathBuf>,
62}
63
64impl CliNotFoundError {
65    /// Create a new CLI not found error
66    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/// Error when connecting to Claude Code CLI
75#[derive(Debug, Error)]
76#[error("Connection error: {message}")]
77pub struct ConnectionError {
78    /// Error message
79    pub message: String,
80}
81
82impl ConnectionError {
83    /// Create a new connection error
84    pub fn new(message: impl Into<String>) -> Self {
85        Self {
86            message: message.into(),
87        }
88    }
89}
90
91/// Error when the CLI process fails
92#[derive(Debug, Error)]
93#[error("Process error (exit code {exit_code:?}): {message}")]
94pub struct ProcessError {
95    /// Error message
96    pub message: String,
97    /// Process exit code
98    pub exit_code: Option<i32>,
99    /// stderr output
100    pub stderr: Option<String>,
101}
102
103impl ProcessError {
104    /// Create a new process error
105    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/// Error when JSON decoding fails
115#[derive(Debug, Error)]
116#[error("JSON decode error: {message}")]
117pub struct JsonDecodeError {
118    /// Error message
119    pub message: String,
120    /// The line that failed to decode
121    pub line: String,
122}
123
124impl JsonDecodeError {
125    /// Create a new JSON decode error
126    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/// Error when message parsing fails
135#[derive(Debug, Error)]
136#[error("Message parse error: {message}")]
137pub struct MessageParseError {
138    /// Error message
139    pub message: String,
140    /// The data that failed to parse
141    pub data: Option<serde_json::Value>,
142}
143
144impl MessageParseError {
145    /// Create a new message parse error
146    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/// Image validation error
155#[derive(Debug, Error)]
156#[error("Image validation error: {message}")]
157pub struct ImageValidationError {
158    /// Error message
159    pub message: String,
160}
161
162impl ImageValidationError {
163    /// Create a new image validation error
164    pub fn new(message: impl Into<String>) -> Self {
165        Self {
166            message: message.into(),
167        }
168    }
169}
170
171/// Result type for the Claude Agent SDK
172pub type Result<T> = std::result::Result<T, ClaudeError>;