#[non_exhaustive]pub struct ApiError {
pub status: u16,
pub code: Option<i64>,
pub message: String,
pub body: String,
pub path: String,
}Expand description
A non-success HTTP response from Alpaca.
Every field is public to read. Building one goes through
from_body, which is also how the transport builds the
ones a caller receives.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.status: u16The HTTP status code.
code: Option<i64>Alpaca’s numeric error code, when the body was JSON carrying one.
message: StringThe human-readable message, falling back to the raw body.
body: StringThe unparsed response body.
path: StringThe request path that produced this error.
Implementations§
Source§impl ApiError
impl ApiError
Sourcepub fn from_body(
status: u16,
path: impl Into<String>,
body: impl Into<String>,
) -> Self
pub fn from_body( status: u16, path: impl Into<String>, body: impl Into<String>, ) -> Self
Builds an ApiError from a status, a path, and the raw response body.
This is the only way to construct one, here and in a caller’s own code:
the struct is #[non_exhaustive], so a field-by-field literal is not
available outside the crate. That is deliberate rather than an oversight
— code and message are read out of
body, and a constructor taking all three separately would
let a caller build an error whose fields contradict each other, which is
a state no response can produce.
It is public so a caller can build one to test their own error handling against, and what they get behaves exactly like a real failure — the same parse, including the degradation below:
use alpaca_sdk::ApiError;
let error = ApiError::from_body(
403,
"/v2/orders",
r#"{"code":40310000,"message":"insufficient buying power"}"#,
);
assert_eq!(error.code, Some(40_310_000));
assert_eq!(error.message, "insufficient buying power");
assert!(!error.is_retried_by_default());A body that is not the JSON object Alpaca normally sends — a gateway’s
HTML, say — leaves code as None and becomes the message verbatim,
rather than failing. An error type that errors while reporting an error
is the worst place to be strict.
let error = ApiError::from_body(502, "/v2/account", "<html>bad gateway</html>");
assert_eq!(error.code, None);
assert_eq!(error.message, "<html>bad gateway</html>");Sourcepub fn is_retried_by_default(&self) -> bool
pub fn is_retried_by_default(&self) -> bool
Whether this status is in the crate’s default retry set.
The default set, not the policy the client that produced this error was
built with — an ApiError does not carry one. A client configured with
RetryConfig::status_codes([500, 502]) gets false here for the 500 it
actually retried, and true for the 429 it did not. The name says
“by default” so that gap is visible at the call site rather than
surprising.
Trait Implementations§
impl Eq for ApiError
Source§impl Error for ApiError
impl Error for ApiError
1.30.0 · 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()
impl StructuralPartialEq for ApiError
Auto Trait Implementations§
impl Freeze for ApiError
impl RefUnwindSafe for ApiError
impl Send for ApiError
impl Sync for ApiError
impl Unpin for ApiError
impl UnsafeUnpin for ApiError
impl UnwindSafe for ApiError
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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 moreSource§impl<T> Key for Twhere
T: Clone,
impl<T> Key for Twhere
T: Clone,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more