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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// https://github.com/rust-lang/rust-clippy/issues/10198
#![allow(clippy::std_instead_of_core)]

use crate::types::Reconnect;
use hyper::http::uri::InvalidUri;
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, 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("Error during handshake request: `{1}`.")]
    HandshakeError(Reconnect, Box<dyn Error + Sync + Send + 'static>),
    #[error("Error during subscribe request: `{1}`.")]
    SubscribeError(Reconnect, Box<dyn Error + Sync + Send + 'static>),
    #[error("Error during connect request: `{1}`.")]
    ConnectError(Reconnect, Box<dyn Error + Sync + Send + 'static>),
    #[error("Error during disconnect request: `{0}`.")]
    DisconnectError(Box<dyn Error + Sync + Send + 'static>),

    #[error("Got unexpected error: `{0}`")]
    UnexpectedError(Box<dyn Error + Sync + Send + 'static>),
}

impl CometdError {
    /// Return advice if server set it in response.
    #[inline]
    pub const fn advice(&self) -> Reconnect {
        match *self {
            CometdError::MissingEndpoint
            | CometdError::InvalidUrl(_)
            | CometdError::InvalidUri(_)
            | CometdError::DisconnectError(_)
            | CometdError::UnexpectedError(_) => Reconnect::None,
            CometdError::HandshakeError(advice, _)
            | CometdError::SubscribeError(advice, _)
            | CometdError::ConnectError(advice, _) => advice,
        }
    }
}

impl CometdError {
    #[inline(always)]
    pub(crate) fn unexpected_error<E: Error + Sync + Send + 'static>(err: E) -> Self {
        Self::UnexpectedError(err.into())
    }

    #[inline(always)]
    pub(crate) fn handshake_error<E: Error + Sync + Send + 'static>(
        advice: Option<Reconnect>,
        err: E,
    ) -> Self {
        Self::HandshakeError(advice.unwrap_or_default(), err.into())
    }

    #[inline(always)]
    pub(crate) fn subscribe_error<E: Error + Sync + Send + 'static>(
        advice: Option<Reconnect>,
        err: E,
    ) -> Self {
        Self::SubscribeError(advice.unwrap_or_default(), err.into())
    }

    #[inline(always)]
    pub(crate) fn connect_error<E: Error + Sync + Send + 'static>(
        advice: Option<Reconnect>,
        err: E,
    ) -> Self {
        Self::ConnectError(advice.unwrap_or_default(), err.into())
    }

    #[inline(always)]
    pub(crate) fn disconnect_error<E: Error + Sync + Send + 'static>(err: E) -> Self {
        Self::DisconnectError(err.into())
    }
}

#[derive(Debug, thiserror::Error)]
pub(crate) enum InnerError {
    #[error("{0}")]
    WrongResponse(Cow<'static, str>),
    #[error("Make handshake before request.")]
    MissingClientId,
}