Skip to content

Commit

Permalink
Make ApiError::code optional
Browse files Browse the repository at this point in the history
RUST_LOG=trace,hyper=warn cargo test -- --ignored api::v2::clock::tests::request_clock_with_invalid_credentials --nocapture
  • Loading branch information
d-e-s-o committed Jun 6, 2023
1 parent 11c7c5b commit 35cafaf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 5 additions & 1 deletion src/data/v2/bars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,11 @@ mod tests {
let err = client.issue::<Get>(&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:?}"),
};
}
Expand Down
6 changes: 5 additions & 1 deletion src/data/v2/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ mod tests {
let err = client.issue::<Get>(&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:?}"),
};
}
Expand Down
6 changes: 5 additions & 1 deletion src/data/v2/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ mod tests {
let err = client.issue::<Get>(&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:?}"),
};
}
Expand Down
11 changes: 9 additions & 2 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@ pub enum ConversionError {
UrlEncode(#[from] UrlEncodeError),
}

fn format_code(code: &Option<u64>) -> 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<u64>,
/// A message as provided by Alpaca.
#[serde(rename = "message")]
pub message: String,
Expand Down

0 comments on commit 35cafaf

Please sign in to comment.