Skip to main content

bitvavo_api_client/
error.rs

1//! Error types returned by the client.
2
3use std::time::Duration;
4use thiserror::Error;
5
6/// Errors produced by the REST and WebSocket clients.
7#[derive(Debug, Error)]
8#[non_exhaustive]
9pub enum Error {
10    /// Transport-level HTTP failure.
11    #[error("http request failed: {0}")]
12    Http(#[from] reqwest::Error),
13
14    /// Transport-level WebSocket failure.
15    /// The error is boxed to keep the size of `Error` small.
16    #[error("websocket error: {0}")]
17    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
18
19    /// The configured transport URL is not valid.
20    #[error("invalid {transport} URL")]
21    InvalidUrl { transport: &'static str },
22
23    /// The configured transport URL does not use TLS.
24    #[error("{transport} URL must use the {required_scheme} scheme")]
25    InsecureTransport {
26        transport: &'static str,
27        required_scheme: &'static str,
28    },
29
30    /// The WebSocket connection did not complete before the configured timeout.
31    #[error("websocket connection timed out after {0:?}")]
32    WebSocketConnectTimeout(Duration),
33
34    /// Failure to serialize or deserialize a payload.
35    #[error("json error: {0}")]
36    Json(#[from] serde_json::Error),
37
38    /// Error response from the Bitvavo API.
39    /// Error codes are documented at <https://docs.bitvavo.com/>.
40    #[error("bitvavo api error {code}: {message}")]
41    Api { code: i64, message: String },
42
43    /// A private endpoint was called without API credentials.
44    #[error("missing api credentials")]
45    MissingCredentials,
46
47    /// The WebSocket connection is no longer usable.
48    #[error("websocket connection closed")]
49    ConnectionClosed,
50}
51
52impl From<tokio_tungstenite::tungstenite::Error> for Error {
53    fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
54        Self::WebSocket(Box::new(err))
55    }
56}
57
58pub type Result<T> = std::result::Result<T, Error>;