ApiErrorResponse

Struct ApiErrorResponse 

Source
pub struct ApiErrorResponse {
    pub code: u32,
    pub message: String,
    pub request_id: Option<String>,
}
Expand description

Detailed API error response from Alpaca.

Fields§

§code: u32

The error code returned by Alpaca.

§message: String

The error message.

§request_id: Option<String>

Optional request ID for debugging.

Implementations§

Source§

impl ApiErrorResponse

Source

pub fn new(code: u32, message: impl Into<String>) -> Self

Creates a new API error response.

Examples found in repository?
examples/base_error_handling.rs (line 174)
134fn demonstrate_error_matching() {
135    let error = AlpacaError::api_with_details(
136        404,
137        "order not found",
138        ApiErrorCode::NotFound,
139        Some("req-12345".to_string()),
140    );
141
142    // Pattern match on error type
143    match &error {
144        AlpacaError::Api {
145            status,
146            message,
147            error_code,
148            request_id,
149        } => {
150            println!("  API Error detected:");
151            println!("    - Status: {}", status);
152            println!("    - Message: {}", message);
153            println!("    - Error code: {:?}", error_code);
154            println!("    - Request ID: {:?}", request_id);
155        }
156        AlpacaError::RateLimit {
157            retry_after_secs, ..
158        } => {
159            println!("  Rate limited, retry after {} seconds", retry_after_secs);
160        }
161        _ => {
162            println!("  Other error: {}", error);
163        }
164    }
165
166    // Use helper methods
167    println!("\n  Helper methods:");
168    println!("    - status_code(): {:?}", error.status_code());
169    println!("    - request_id(): {:?}", error.request_id());
170    println!("    - is_retryable(): {}", error.is_retryable());
171
172    // API error response parsing
173    let response =
174        ApiErrorResponse::new(40410000, "resource not found").with_request_id("req-67890");
175    println!("\n  Parsed API response:");
176    println!("    - Code: {}", response.code);
177    println!("    - Message: {}", response.message);
178    println!("    - Typed code: {:?}", response.error_code());
179}
Source

pub fn with_request_id(self, request_id: impl Into<String>) -> Self

Sets the request ID.

Examples found in repository?
examples/base_error_handling.rs (line 174)
134fn demonstrate_error_matching() {
135    let error = AlpacaError::api_with_details(
136        404,
137        "order not found",
138        ApiErrorCode::NotFound,
139        Some("req-12345".to_string()),
140    );
141
142    // Pattern match on error type
143    match &error {
144        AlpacaError::Api {
145            status,
146            message,
147            error_code,
148            request_id,
149        } => {
150            println!("  API Error detected:");
151            println!("    - Status: {}", status);
152            println!("    - Message: {}", message);
153            println!("    - Error code: {:?}", error_code);
154            println!("    - Request ID: {:?}", request_id);
155        }
156        AlpacaError::RateLimit {
157            retry_after_secs, ..
158        } => {
159            println!("  Rate limited, retry after {} seconds", retry_after_secs);
160        }
161        _ => {
162            println!("  Other error: {}", error);
163        }
164    }
165
166    // Use helper methods
167    println!("\n  Helper methods:");
168    println!("    - status_code(): {:?}", error.status_code());
169    println!("    - request_id(): {:?}", error.request_id());
170    println!("    - is_retryable(): {}", error.is_retryable());
171
172    // API error response parsing
173    let response =
174        ApiErrorResponse::new(40410000, "resource not found").with_request_id("req-67890");
175    println!("\n  Parsed API response:");
176    println!("    - Code: {}", response.code);
177    println!("    - Message: {}", response.message);
178    println!("    - Typed code: {:?}", response.error_code());
179}
Source

pub fn error_code(&self) -> ApiErrorCode

Returns the typed error code.

Examples found in repository?
examples/base_error_handling.rs (line 178)
134fn demonstrate_error_matching() {
135    let error = AlpacaError::api_with_details(
136        404,
137        "order not found",
138        ApiErrorCode::NotFound,
139        Some("req-12345".to_string()),
140    );
141
142    // Pattern match on error type
143    match &error {
144        AlpacaError::Api {
145            status,
146            message,
147            error_code,
148            request_id,
149        } => {
150            println!("  API Error detected:");
151            println!("    - Status: {}", status);
152            println!("    - Message: {}", message);
153            println!("    - Error code: {:?}", error_code);
154            println!("    - Request ID: {:?}", request_id);
155        }
156        AlpacaError::RateLimit {
157            retry_after_secs, ..
158        } => {
159            println!("  Rate limited, retry after {} seconds", retry_after_secs);
160        }
161        _ => {
162            println!("  Other error: {}", error);
163        }
164    }
165
166    // Use helper methods
167    println!("\n  Helper methods:");
168    println!("    - status_code(): {:?}", error.status_code());
169    println!("    - request_id(): {:?}", error.request_id());
170    println!("    - is_retryable(): {}", error.is_retryable());
171
172    // API error response parsing
173    let response =
174        ApiErrorResponse::new(40410000, "resource not found").with_request_id("req-67890");
175    println!("\n  Parsed API response:");
176    println!("    - Code: {}", response.code);
177    println!("    - Message: {}", response.message);
178    println!("    - Typed code: {:?}", response.error_code());
179}

Trait Implementations§

Source§

impl Clone for ApiErrorResponse

Source§

fn clone(&self) -> ApiErrorResponse

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ApiErrorResponse

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ApiErrorResponse

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for ApiErrorResponse

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,