signalrs_client_custom_auth/
error.rs

1//! Top level crate errors
2
3use crate::{hub::error::HubError, messages::SerializationError};
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum ClientError {
8    /// Issued request was malformed
9    #[error("malformed {direction}")]
10    Malformed {
11        direction: &'static str,
12        #[source]
13        source: SerializationError,
14    },
15
16    /// There was an error during invocation on client-side hub
17    #[error(transparent)]
18    Hub {
19        #[from]
20        source: HubError,
21    },
22
23    /// SignalR protocol was violated
24    ///
25    /// Probably by callee, see message for details.
26    #[error("protocol violation: {message}")]
27    ProtocolError { message: String },
28
29    /// There was an error
30    #[error("it is no longer possible to receive response: {message}")]
31    NoResponse { message: String },
32
33    /// Error returned from the server
34    #[error("error returned from the server: {message}")]
35    Result { message: String },
36
37    /// Client cannot reach transport
38    ///
39    /// There could be abrupt close of underlying transport (WebSockets or other)
40    #[error("transport layer is inavailable")]
41    TransportInavailable { message: String },
42
43    #[error("handshake error")]
44    Handshake { message: String },
45}
46
47impl ClientError {
48    pub fn protocol_violation(message: impl ToString) -> ClientError {
49        ClientError::ProtocolError {
50            message: message.to_string(),
51        }
52    }
53
54    pub fn no_response(message: impl ToString) -> ClientError {
55        ClientError::NoResponse {
56            message: message.to_string(),
57        }
58    }
59
60    pub fn malformed_request(source: SerializationError) -> ClientError {
61        ClientError::Malformed {
62            direction: "request",
63            source,
64        }
65    }
66
67    pub fn malformed_response(source: SerializationError) -> ClientError {
68        ClientError::Malformed {
69            direction: "request",
70            source,
71        }
72    }
73
74    pub fn result(message: impl ToString) -> ClientError {
75        ClientError::Result {
76            message: message.to_string(),
77        }
78    }
79
80    pub fn transport(message: impl ToString) -> ClientError {
81        ClientError::TransportInavailable {
82            message: message.to_string(),
83        }
84    }
85
86    pub fn handshake(message: impl ToString) -> ClientError {
87        ClientError::Handshake {
88            message: message.to_string(),
89        }
90    }
91}