#[non_exhaustive]pub enum ApiError<E: Error = NoDomainError> {
Http(Error),
HttpBuild(HttpError),
Json(Error),
Api {
status: u16,
message: String,
},
RateLimited {
retry_after: Option<u64>,
},
ServerError {
status: u16,
message: String,
},
Url(ParseError),
Domain(E),
}Expand description
Generic API error type with support for domain-specific errors
This error type covers the common error cases across all API clients:
- HTTP transport errors
- JSON parsing errors
- API response errors (4xx)
- Rate limiting (429)
- Server errors (5xx)
Domain-specific errors can be added via the generic parameter E.
Use ApiError (without a type parameter) when no domain-specific errors
are needed.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Http(Error)
HTTP request error
HttpBuild(HttpError)
HTTP client build error
Json(Error)
JSON parsing error
Api
API returned an error response (4xx, excluding 429)
RateLimited
Rate limit exceeded (429)
ServerError
Server error (5xx)
Url(ParseError)
URL parsing error
Domain(E)
Domain-specific error
Implementations§
Source§impl<E: Error> ApiError<E>
impl<E: Error> ApiError<E>
Sourcepub fn rate_limited(retry_after: Option<u64>) -> Self
pub fn rate_limited(retry_after: Option<u64>) -> Self
Create a rate limited error
Sourcepub fn server_error(status: u16, message: impl Into<String>) -> Self
pub fn server_error(status: u16, message: impl Into<String>) -> Self
Create a server error
Sourcepub fn from_response(status: u16, body: &str, retry_after: Option<u64>) -> Self
pub fn from_response(status: u16, body: &str, retry_after: Option<u64>) -> Self
Create from HTTP response status and body
Automatically categorizes the error based on status code:
- 429 -> RateLimited
- 500-599 -> ServerError
- Other -> Api
Sourcepub fn is_retryable(&self) -> bool
pub fn is_retryable(&self) -> bool
Check if this error is retryable
Returns true for:
- Rate limited errors
- Server errors (5xx)
- HTTP transport errors
Sourcepub fn retry_after(&self) -> Option<Duration>
pub fn retry_after(&self) -> Option<Duration>
Get retry-after duration if available
Sourcepub fn status_code(&self) -> Option<u16>
pub fn status_code(&self) -> Option<u16>
Get the HTTP status code if this is an API or server error
Trait Implementations§
Source§impl<E> Error for ApiError<E>
impl<E> Error for ApiError<E>
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
Source§impl<E: Error> From<ParseError> for ApiError<E>
impl<E: Error> From<ParseError> for ApiError<E>
Source§fn from(source: ParseError) -> Self
fn from(source: ParseError) -> Self
Source§impl<E: Error> RetryableError for ApiError<E>
impl<E: Error> RetryableError for ApiError<E>
Source§fn is_retryable(&self) -> bool
fn is_retryable(&self) -> bool
Source§fn retry_after(&self) -> Option<Duration>
fn retry_after(&self) -> Option<Duration>
Auto Trait Implementations§
impl<E> Freeze for ApiError<E>where
E: Freeze,
impl<E = NoDomainError> !RefUnwindSafe for ApiError<E>
impl<E> Send for ApiError<E>where
E: Send,
impl<E> Sync for ApiError<E>where
E: Sync,
impl<E> Unpin for ApiError<E>where
E: Unpin,
impl<E = NoDomainError> !UnwindSafe for ApiError<E>
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.