Skip to content

Commit

Permalink
Cleanup error handling (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
wboayue authored Dec 26, 2024
1 parent b65b781 commit bbe2652
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ibapi"
version = "1.0.8"
version = "1.0.9"
edition = "2021"
authors = ["Wil Boayue <wil@wsbsolutions.com>"]
description = "A Rust implementation of the Interactive Brokers TWS API, providing a reliable and user friendly interface for TWS and IB Gateway. Designed with a focus on simplicity and performance."
Expand Down
4 changes: 4 additions & 0 deletions examples/market_depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ fn main() {
for row in &subscription {
println!("row: {row:?}")
}

if let Some(error) = subscription.error() {
println!("error: {:?}", error);
}
}
5 changes: 2 additions & 3 deletions src/market_data/realtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ impl ToField for WhatToShow {
pub enum MarketDepths {
MarketDepth(MarketDepth),
MarketDepthL2(MarketDepthL2),
Notice(Notice),
}

#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -252,8 +251,8 @@ impl DataStream<MarketDepths> for MarketDepths {
client.server_version,
message,
)?)),
IncomingMessages::Error => Ok(MarketDepths::Notice(Notice::from(message))),
_ => Err(Error::NotImplemented),
IncomingMessages::Error => Err(Error::from(message.clone())),
_ => Err(Error::UnexpectedResponse(message.clone())),
}
}

Expand Down
65 changes: 20 additions & 45 deletions src/wsh/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ fn test_wsh_event_data_by_contract() {

let client = Client::stubbed(message_bus, server_versions::WSH_EVENT_DATA_FILTERS_DATE);
let result = wsh_event_data_by_contract(
&client,
&client,
12345,
Some(date!(2024-01-01)),
Some(date!(2024-12-31)),
Some(date!(2024 - 01 - 01)),
Some(date!(2024 - 12 - 31)),
Some(100),
Some(AutoFill {
competitors: true,
Expand All @@ -49,10 +49,7 @@ fn test_wsh_event_data_by_contract() {
);

let request_messages = client.message_bus.request_messages();
assert_eq!(
request_messages[0].encode_simple(),
"102|9000|12345||1|1|1|20240101|20241231|100|"
);
assert_eq!(request_messages[0].encode_simple(), "102|9000|12345||1|1|1|20240101|20241231|100|");

assert!(result.is_ok(), "failed to request wsh event data: {}", result.err().unwrap());
assert_eq!(
Expand Down Expand Up @@ -106,10 +103,7 @@ fn test_wsh_event_data_by_filter() {
);

let request_messages = client.message_bus.request_messages();
assert_eq!(
request_messages[0].encode_simple(),
"102|9000||filter=value|1|0|1|||100|"
);
assert_eq!(request_messages[0].encode_simple(), "102|9000||filter=value|1|0|1|||100|");

assert!(result.is_ok(), "failed to request wsh event data by filter: {}", result.err().unwrap());
}
Expand All @@ -126,10 +120,7 @@ fn test_wsh_event_data_by_filter_no_autofill() {
let result = wsh_event_data_by_filter(&client, filter, None, None);

let request_messages = client.message_bus.request_messages();
assert_eq!(
request_messages[0].encode_simple(),
"102|9000||filter=value|0|0|0|"
);
assert_eq!(request_messages[0].encode_simple(), "102|9000||filter=value|0|0|0|");

assert!(result.is_ok(), "failed to request wsh event data by filter: {}", result.err().unwrap());
}
Expand Down Expand Up @@ -168,39 +159,35 @@ fn test_invalid_server_version_wsh_event_data_date_filters() {
});

let client = Client::stubbed(message_bus, server_versions::WSH_EVENT_DATA_FILTERS);
let result = wsh_event_data_by_contract(
&client,
12345,
Some(date!(2024-01-01)),
Some(date!(2024-12-31)),
Some(100),
None,
);
let result = wsh_event_data_by_contract(&client, 12345, Some(date!(2024 - 01 - 01)), Some(date!(2024 - 12 - 31)), Some(100), None);

assert!(matches!(result, Err(Error::ServerVersion(_, _, _))));
}

#[test]
fn test_autofill_is_specified() {
assert!(!AutoFill::default().is_specified());

assert!(AutoFill {
competitors: true,
portfolio: false,
watchlist: false,
}.is_specified());

}
.is_specified());

assert!(AutoFill {
competitors: false,
portfolio: true,
watchlist: false,
}.is_specified());

}
.is_specified());

assert!(AutoFill {
competitors: false,
portfolio: false,
watchlist: true,
}.is_specified());
}
.is_specified());
}

#[test]
Expand Down Expand Up @@ -248,16 +235,7 @@ fn test_encode_request_wsh_event_data() {
use super::encoders::encode_request_wsh_event_data;

// Test with minimal params
let result = encode_request_wsh_event_data(
server_versions::WSHE_CALENDAR,
9000,
Some(12345),
None,
None,
None,
None,
None,
);
let result = encode_request_wsh_event_data(server_versions::WSHE_CALENDAR, 9000, Some(12345), None, None, None, None, None);
assert!(result.is_ok());
assert_eq!(result.unwrap().encode_simple(), "102|9000|12345|");

Expand All @@ -267,8 +245,8 @@ fn test_encode_request_wsh_event_data() {
9000,
Some(12345),
Some("filter"),
Some(date!(2024-01-01)),
Some(date!(2024-12-31)),
Some(date!(2024 - 01 - 01)),
Some(date!(2024 - 12 - 31)),
Some(100),
Some(AutoFill {
competitors: true,
Expand All @@ -277,10 +255,7 @@ fn test_encode_request_wsh_event_data() {
}),
);
assert!(result.is_ok());
assert_eq!(
result.unwrap().encode_simple(),
"102|9000|12345|filter|1|0|1|20240101|20241231|100|"
);
assert_eq!(result.unwrap().encode_simple(), "102|9000|12345|filter|1|0|1|20240101|20241231|100|");
}

#[test]
Expand Down

0 comments on commit bbe2652

Please sign in to comment.