pub enum Error {
Http(Error),
Json(Error),
Api {
status: u16,
message: String,
error_type: Option<String>,
},
RateLimit {
retry_after: Option<u64>,
message: String,
},
InvalidRequest(String),
Authentication(String),
Server {
status: u16,
message: String,
},
Network(String),
StreamParse(String),
}Expand description
Main error type for Claude SDK operations.
This enum covers all possible error conditions when using the SDK.
Use Error::is_retryable to check if an error can be retried.
§Example
use claude_sdk::Error;
fn handle_error(err: Error) {
if err.is_retryable() {
println!("Can retry after {:?}s", err.retry_after());
} else {
println!("Fatal error: {}", err);
}
}Variants§
Http(Error)
HTTP request failed.
This wraps errors from the underlying HTTP client (reqwest). May be retryable depending on the specific error.
Json(Error)
Failed to parse JSON response.
This typically indicates a bug or API contract change.
Api
API returned an error response.
Contains the HTTP status code and error message from the API.
Check error_type for specific error categories like “invalid_request_error”.
Fields
RateLimit
Rate limit exceeded (HTTP 429).
The API has rate-limited your request. Check retry_after for the
recommended wait time before retrying.
Fields
InvalidRequest(String)
Invalid request.
The request was malformed or missing required fields. This is not retryable - fix the request before trying again.
Authentication(String)
Authentication failed.
The API key is invalid, expired, or missing required permissions. Not retryable - verify your API key configuration.
Server
Server error (HTTP 5xx).
The API server encountered an internal error. These are typically transient and can be retried with exponential backoff.
Network(String)
Network error.
Connection failed, timed out, or was interrupted. Usually retryable after a brief delay.
StreamParse(String)
SSE stream parsing error.
Failed to parse a server-sent event during streaming. May indicate a malformed response or connection issue.
Implementations§
Source§impl Error
impl Error
Sourcepub fn is_retryable(&self) -> bool
pub fn is_retryable(&self) -> bool
Check if this error is retryable.
Returns true for transient errors that may succeed on retry:
- Rate limits (with backoff)
- Server errors (5xx)
- Network errors
§Example
use claude_sdk::Error;
fn should_retry(err: &Error) -> bool {
err.is_retryable()
}Sourcepub fn retry_after(&self) -> Option<u64>
pub fn retry_after(&self) -> Option<u64>
Get retry-after duration in seconds if available.
Only returns a value for Error::RateLimit errors that include
a Retry-After header from the API.
§Example
use claude_sdk::Error;
use std::time::Duration;
async fn wait_and_retry(err: &Error) {
if let Some(seconds) = err.retry_after() {
tokio::time::sleep(Duration::from_secs(seconds)).await;
}
}Trait Implementations§
Source§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Auto Trait Implementations§
impl !RefUnwindSafe for Error
impl !UnwindSafe for Error
impl Freeze for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more