longport_wscli/
error.rs

1use prost::DecodeError;
2use tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode;
3
4/// Connection close reason
5#[derive(Debug)]
6pub struct WsCloseReason {
7    /// Close code
8    pub code: CloseCode,
9
10    /// Reason string
11    pub message: String,
12}
13
14/// Detail message for response error
15#[derive(Debug)]
16pub struct WsResponseErrorDetail {
17    /// Error code
18    pub code: u64,
19    /// Error message
20    pub msg: String,
21}
22
23/// Websocket client error type
24#[derive(Debug, thiserror::Error)]
25pub enum WsClientError {
26    /// Unexpected response
27    #[error("unexpected response")]
28    UnexpectedResponse,
29
30    /// Decode message error
31    #[error("decode message error")]
32    Decode(#[from] DecodeError),
33
34    /// Connect timeout
35    #[error("connect timeout")]
36    ConnectTimeout,
37
38    /// Request timeout
39    #[error("request timeout")]
40    RequestTimeout,
41
42    /// Connection closed
43    #[error("connection closed")]
44    ConnectionClosed {
45        /// The reason the connection was closed
46        reason: Option<WsCloseReason>,
47    },
48
49    /// Client is closed
50    #[error("Client is closed")]
51    ClientClosed,
52
53    /// The server responded a status code not equal to `0`
54    #[error("response error: {status}: detail:{detail:?}")]
55    ResponseError {
56        /// Status code
57        status: u8,
58        /// Error detail
59        detail: Option<WsResponseErrorDetail>,
60    },
61
62    /// The request has been cancelled
63    #[error("cancelled")]
64    Cancelled,
65
66    /// Invalid url
67    #[error(transparent)]
68    InvalidUrl(#[from] url::ParseError),
69
70    /// Websocket error
71    #[error(transparent)]
72    Websocket(#[from] tokio_tungstenite::tungstenite::Error),
73}
74
75/// Websocket client result type
76pub type WsClientResult<T, E = WsClientError> = std::result::Result<T, E>;