Skip to main content

truefix_twsapi_client/
error.rs

1use thiserror::Error;
2
3/// Result type used by the TWS API client crate.
4pub type TwsApiResult<T> = Result<T, TwsApiError>;
5
6/// Errors produced while encoding, decoding, or transporting TWS API frames.
7#[derive(Debug, Error)]
8pub enum TwsApiError {
9    /// A caller attempted to encode a value containing non-printable ASCII.
10    #[error("TWS API fields must be printable ASCII: {0:?}")]
11    NonPrintableAscii(String),
12    /// A caller attempted to send a missing field value.
13    #[error("cannot encode a missing TWS API field")]
14    MissingField,
15    /// A frame length prefix exceeded the supported platform size.
16    #[error("TWS API frame length {0} is too large for this platform")]
17    FrameTooLarge(u32),
18    /// A message was incomplete.
19    #[error("incomplete TWS API frame: need {needed} bytes, have {available} bytes")]
20    IncompleteFrame {
21        /// Number of bytes needed to finish the frame.
22        needed: usize,
23        /// Number of bytes currently available.
24        available: usize,
25    },
26    /// The peer returned a malformed handshake response.
27    #[error("malformed TWS API handshake response")]
28    MalformedHandshake,
29    /// The peer advertised an unsupported server version.
30    #[error("TWS/Gateway server version {server_version} is below required minimum {min_version}")]
31    UnsupportedServerVersion {
32        /// Advertised server version.
33        server_version: i32,
34        /// Required minimum version.
35        min_version: i32,
36    },
37    /// A request used a field that the negotiated server version does not support.
38    #[error(
39        "TWS/Gateway server version {server_version} is below required minimum {min_version} for {parameter}"
40    )]
41    UnsupportedRequestParameter {
42        /// Advertised server version.
43        server_version: i32,
44        /// Required minimum version.
45        min_version: i32,
46        /// Unsupported parameter name.
47        parameter: &'static str,
48    },
49    /// A network operation failed.
50    #[error("TWS API I/O error: {0}")]
51    Io(#[from] std::io::Error),
52    /// An integer field could not be parsed.
53    #[error("invalid TWS API integer field {field:?}: {source}")]
54    InvalidInteger {
55        /// Field content.
56        field: String,
57        /// Parse failure.
58        source: std::num::ParseIntError,
59    },
60    /// A decimal field could not be parsed.
61    #[error("invalid TWS API decimal field {field:?}: {source}")]
62    InvalidDecimal {
63        /// Field content.
64        field: String,
65        /// Parse failure.
66        source: rust_decimal::Error,
67    },
68    /// A protobuf payload could not be decoded.
69    #[error("invalid TWS API protobuf payload: {0}")]
70    ProtobufDecode(#[from] prost::DecodeError),
71}