cometd_client/types/
error.rs

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