diff --git a/src/client.rs b/src/client.rs index 9837f076..c49a7a68 100644 --- a/src/client.rs +++ b/src/client.rs @@ -376,7 +376,7 @@ mod tests { match err { RequestError::Endpoint(GetNotFoundError::UnexpectedStatus(status, message)) => { let expected = ApiError { - code: 40410000, + code: Some(40410000), message: "endpoint not found".to_string(), }; assert_eq!(message, Ok(expected)); diff --git a/src/data/v2/bars.rs b/src/data/v2/bars.rs index a5c8d405..d9ad9acd 100644 --- a/src/data/v2/bars.rs +++ b/src/data/v2/bars.rs @@ -434,7 +434,11 @@ mod tests { let err = client.issue::(&request).await.unwrap_err(); match err { // 42210000 is the error code reported for "invalid symbol". - RequestError::Endpoint(GetError::InvalidInput(Ok(message))) if message.code == 42210000 => (), + RequestError::Endpoint(GetError::InvalidInput(Ok(message))) + if message.code == Some(42210000) => + { + () + }, _ => panic!("Received unexpected error: {err:?}"), }; } diff --git a/src/data/v2/quotes.rs b/src/data/v2/quotes.rs index 480cfd9d..6cdcb58d 100644 --- a/src/data/v2/quotes.rs +++ b/src/data/v2/quotes.rs @@ -172,7 +172,11 @@ mod tests { let err = client.issue::(&request).await.unwrap_err(); match err { // 42210000 is the error code reported for "invalid symbol". - RequestError::Endpoint(GetError::InvalidInput(Ok(message))) if message.code == 42210000 => (), + RequestError::Endpoint(GetError::InvalidInput(Ok(message))) + if message.code == Some(42210000) => + { + () + }, _ => panic!("Received unexpected error: {err:?}"), }; } diff --git a/src/data/v2/trades.rs b/src/data/v2/trades.rs index e02a2df7..f8cdb405 100644 --- a/src/data/v2/trades.rs +++ b/src/data/v2/trades.rs @@ -297,7 +297,11 @@ mod tests { let err = client.issue::(&request).await.unwrap_err(); match err { // 42210000 is the error code reported for "invalid symbol". - RequestError::Endpoint(GetError::InvalidInput(Ok(message))) if message.code == 42210000 => (), + RequestError::Endpoint(GetError::InvalidInput(Ok(message))) + if message.code == Some(42210000) => + { + () + }, _ => panic!("Received unexpected error: {err:?}"), }; } diff --git a/src/endpoint.rs b/src/endpoint.rs index 08770d3f..6b55a413 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -19,17 +19,24 @@ pub enum ConversionError { UrlEncode(#[from] UrlEncodeError), } +fn format_code(code: &Option) -> String { + if let Some(code) = code { + format!(" ({code})") + } else { + String::new() + } +} /// An error as reported by API endpoints. // Note that actually this type should probably be specific to the API // version in question. However, at this point we only support v2, so we // luck out here. #[derive(Clone, Debug, Deserialize, Error, Eq, PartialEq)] -#[error("{message} ({code})")] +#[error("{message}{}", format_code(.code))] pub struct ApiError { /// An error code as provided by Alpaca. #[serde(rename = "code")] - pub code: u64, + pub code: Option, /// A message as provided by Alpaca. #[serde(rename = "message")] pub message: String,