Skip to main content

tse_client/
error.rs

1//! Error types for the crate.
2
3use thiserror::Error;
4
5/// Top-level error type returned by the library.
6#[derive(Error, Debug)]
7pub enum Error {
8    #[error("http request failed: {0}")]
9    Http(#[from] reqwest::Error),
10
11    #[error("io error: {0}")]
12    Io(#[from] std::io::Error),
13
14    #[error("json parse error: {0}")]
15    Json(#[from] serde_json::Error),
16
17    #[error("invalid server response: {0}")]
18    InvalidResponse(String),
19
20    #[error("failed request: {title}: {detail}")]
21    FailedRequest { title: String, detail: String },
22
23    #[error("invalid data: {0}")]
24    InvalidData(String),
25
26    #[error("invalid url: {0}")]
27    InvalidUrl(String),
28}
29
30/// Domain errors that mirror the JS `result.error` object shapes.
31/// These are *not* fatal `Result::Err` values; they are returned inside
32/// the successful result so callers can inspect partial outcomes.
33#[derive(Debug, Clone)]
34pub enum ResultError {
35    /// code 1 — a server/request failure occurred.
36    Request { title: String, detail: String },
37    /// code 2 — one or more requested symbols were not found.
38    IncorrectSymbol { symbols: Vec<String> },
39    /// code 3 — prices for some symbols failed to update.
40    IncompletePriceUpdate {
41        fails: Vec<String>,
42        succs: Vec<String>,
43    },
44    /// code 4 — intraday data for some symbols failed to update.
45    IncompleteIntradayUpdate {
46        fails: std::collections::HashMap<String, Vec<String>>,
47        succs: std::collections::HashMap<String, Vec<String>>,
48    },
49}
50
51impl ResultError {
52    /// The numeric error code matching the original JS API.
53    pub fn code(&self) -> u8 {
54        match self {
55            ResultError::Request { .. } => 1,
56            ResultError::IncorrectSymbol { .. } => 2,
57            ResultError::IncompletePriceUpdate { .. } => 3,
58            ResultError::IncompleteIntradayUpdate { .. } => 4,
59        }
60    }
61}
62
63pub type Result<T> = std::result::Result<T, Error>;