1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#![allow(clippy::std_instead_of_core)]
use crate::types::Reconnect;
use hyper::{http::uri::InvalidUri, Error as HyperError, StatusCode};
use serde_json::Error as JsonError;
use std::{borrow::Cow, error::Error};
use url::ParseError as UrlParseError;
#[allow(missing_docs)]
pub type CometdResult<T> = Result<T, CometdError>;
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone)]
pub enum ErrorKind {
Handshake,
Subscribe,
Connect,
Disconnect,
}
#[allow(missing_docs)]
#[derive(Debug, thiserror::Error)]
pub enum CometdError {
#[error("Endpoint wasn't set in builder.")]
MissingEndpoint,
#[error("Url parse error: `{0}`.")]
InvalidUrl(#[from] UrlParseError),
#[error("Url parse error: `{0}`.")]
InvalidUri(#[from] InvalidUri),
#[error("Got request error at {0:?}: `{1}`.")]
Request(ErrorKind, HyperError),
#[error("Got request timeout at {0:?}.")]
RequestTimeout(ErrorKind),
#[error("Got unsuccessful StatusCode error at {0:?}: `{1}`.")]
StatusCode(ErrorKind, StatusCode, Vec<u8>),
#[error("Got fetching body error at {0:?}: `{1}`.")]
FetchBody(ErrorKind, HyperError),
#[error("Got parsing body error at {0:?}: `{1}`.")]
ParseBody(ErrorKind, JsonError),
#[error("Got wrong response at {0:?}: `{2}`")]
WrongResponse(ErrorKind, Reconnect, Cow<'static, str>),
#[error("Make handshake before {0:?} request.")]
MissingClientId(ErrorKind),
#[error("Got unexpected error: `{0}`")]
Unexpected(Box<dyn Error + Sync + Send + 'static>),
}
impl CometdError {
#[inline(always)]
pub(crate) fn wrong_response<E>(kind: ErrorKind, advice: Reconnect, error_message: E) -> Self
where
Cow<'static, str>: From<E>,
{
Self::WrongResponse(kind, advice, Cow::from(error_message))
}
#[inline(always)]
pub(crate) fn unexpected<E: Error + Sync + Send + 'static>(error: E) -> Self {
Self::Unexpected(Box::from(error))
}
}