awc/
error.rs

1//! HTTP client errors
2
3// TODO: figure out how best to expose http::Error vs actix_http::Error
4pub use actix_http::{
5    error::{HttpError, PayloadError},
6    header::HeaderValue,
7    ws::{HandshakeError as WsHandshakeError, ProtocolError as WsProtocolError},
8    StatusCode,
9};
10
11use derive_more::{Display, From};
12use serde_json::error::Error as JsonError;
13
14pub use crate::client::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError};
15
16// TODO: address display, error, and from impls
17
18/// Websocket client error
19#[derive(Debug, Display, From)]
20pub enum WsClientError {
21    /// Invalid response status
22    #[display(fmt = "Invalid response status")]
23    InvalidResponseStatus(StatusCode),
24
25    /// Invalid upgrade header
26    #[display(fmt = "Invalid upgrade header")]
27    InvalidUpgradeHeader,
28
29    /// Invalid connection header
30    #[display(fmt = "Invalid connection header")]
31    InvalidConnectionHeader(HeaderValue),
32
33    /// Missing Connection header
34    #[display(fmt = "Missing Connection header")]
35    MissingConnectionHeader,
36
37    /// Missing Sec-Websocket-Accept header
38    #[display(fmt = "Missing Sec-Websocket-Accept header")]
39    MissingWebSocketAcceptHeader,
40
41    /// Invalid challenge response
42    #[display(fmt = "Invalid challenge response")]
43    InvalidChallengeResponse([u8; 28], HeaderValue),
44
45    /// Protocol error
46    #[display(fmt = "{}", _0)]
47    Protocol(WsProtocolError),
48
49    /// Send request error
50    #[display(fmt = "{}", _0)]
51    SendRequest(SendRequestError),
52}
53
54impl std::error::Error for WsClientError {}
55
56impl From<InvalidUrl> for WsClientError {
57    fn from(err: InvalidUrl) -> Self {
58        WsClientError::SendRequest(err.into())
59    }
60}
61
62impl From<HttpError> for WsClientError {
63    fn from(err: HttpError) -> Self {
64        WsClientError::SendRequest(err.into())
65    }
66}
67
68/// A set of errors that can occur during parsing json payloads
69#[derive(Debug, Display, From)]
70pub enum JsonPayloadError {
71    /// Content type error
72    #[display(fmt = "Content type error")]
73    ContentType,
74    /// Deserialize error
75    #[display(fmt = "Json deserialize error: {}", _0)]
76    Deserialize(JsonError),
77    /// Payload error
78    #[display(fmt = "Error that occur during reading payload: {}", _0)]
79    Payload(PayloadError),
80}
81
82impl std::error::Error for JsonPayloadError {}