fyers_rs/error.rs
1//! Error types used by `fyers-rs`.
2
3use thiserror::Error;
4
5/// Convenient crate-local result type.
6pub type Result<T> = std::result::Result<T, FyersError>;
7
8/// Error type for client configuration, transport, serialization, and broker responses.
9#[derive(Debug, Error)]
10pub enum FyersError {
11 /// A required client configuration value was not provided.
12 #[error("missing required configuration: {field}")]
13 MissingConfig {
14 /// Name of the missing configuration field.
15 field: &'static str,
16 },
17
18 /// A URL could not be parsed.
19 #[error("invalid URL: {0}")]
20 Url(#[from] url::ParseError),
21
22 /// The underlying HTTP client failed.
23 #[error("HTTP client error: {0}")]
24 HttpClient(#[from] reqwest::Error),
25
26 /// JSON serialization or deserialization failed.
27 #[error("JSON error: {0}")]
28 Json(#[from] serde_json::Error),
29
30 /// Fyers returned a documented broker/API error envelope or non-success HTTP status.
31 #[error("broker error: HTTP {status}, code {code:?}, s {s:?}, message {message:?}")]
32 Broker {
33 /// HTTP status returned by the broker.
34 status: reqwest::StatusCode,
35 /// Standard retry delay header documented for order rate limits, when present.
36 retry_after: Option<Box<str>>,
37 /// Millisecond retry delay header documented for order rate limits, when present.
38 retry_after_ms: Option<Box<str>>,
39 /// Broker-specific numeric code, when present.
40 code: Option<i64>,
41 /// Broker status string, when present.
42 s: Option<Box<str>>,
43 /// Broker message, when present.
44 message: Option<Box<str>>,
45 /// Raw response body for diagnostics.
46 body: Box<str>,
47 },
48
49 /// WebSocket transport failed.
50 #[error("WebSocket error: {0}")]
51 WebSocket(#[source] Box<tokio_tungstenite::tungstenite::Error>),
52
53 /// Builder or request validation failed before a broker call was made.
54 #[error("validation error: {0}")]
55 Validation(String),
56}
57
58impl From<tokio_tungstenite::tungstenite::Error> for FyersError {
59 fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
60 Self::WebSocket(Box::new(error))
61 }
62}