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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::{fmt, io};

use actix_http::error::{HttpError, ParseError};
#[cfg(feature = "openssl")]
use actix_tls::accept::openssl::reexports::Error as OpensslError;
use derive_more::{Display, From};

use crate::BoxError;

/// A set of errors that can occur while connecting to an HTTP host
#[derive(Debug, Display, From)]
#[non_exhaustive]
pub enum ConnectError {
    /// SSL feature is not enabled
    #[display(fmt = "SSL is not supported")]
    SslIsNotSupported,

    /// SSL error
    #[cfg(feature = "openssl")]
    #[display(fmt = "{}", _0)]
    SslError(OpensslError),

    /// Failed to resolve the hostname
    #[display(fmt = "Failed resolving hostname: {}", _0)]
    Resolver(Box<dyn std::error::Error>),

    /// No dns records
    #[display(fmt = "No DNS records found for the input")]
    NoRecords,

    /// Http2 error
    #[display(fmt = "{}", _0)]
    H2(h2::Error),

    /// Connecting took too long
    #[display(fmt = "Timeout while establishing connection")]
    Timeout,

    /// Connector has been disconnected
    #[display(fmt = "Internal error: connector has been disconnected")]
    Disconnected,

    /// Unresolved host name
    #[display(fmt = "Connector received `Connect` method with unresolved host")]
    Unresolved,

    /// Connection io error
    #[display(fmt = "{}", _0)]
    Io(io::Error),
}

impl std::error::Error for ConnectError {}

impl From<actix_tls::connect::ConnectError> for ConnectError {
    fn from(err: actix_tls::connect::ConnectError) -> ConnectError {
        match err {
            actix_tls::connect::ConnectError::Resolver(e) => ConnectError::Resolver(e),
            actix_tls::connect::ConnectError::NoRecords => ConnectError::NoRecords,
            actix_tls::connect::ConnectError::InvalidInput => panic!(),
            actix_tls::connect::ConnectError::Unresolved => ConnectError::Unresolved,
            actix_tls::connect::ConnectError::Io(e) => ConnectError::Io(e),
        }
    }
}

#[derive(Debug, Display, From)]
#[non_exhaustive]
pub enum InvalidUrl {
    #[display(fmt = "Missing URL scheme")]
    MissingScheme,

    #[display(fmt = "Unknown URL scheme")]
    UnknownScheme,

    #[display(fmt = "Missing host name")]
    MissingHost,

    #[display(fmt = "URL parse error: {}", _0)]
    HttpError(http::Error),
}

impl std::error::Error for InvalidUrl {}

/// A set of errors that can occur during request sending and response reading
#[derive(Debug, Display, From)]
#[non_exhaustive]
pub enum SendRequestError {
    /// Invalid URL
    #[display(fmt = "Invalid URL: {}", _0)]
    Url(InvalidUrl),

    /// Failed to connect to host
    #[display(fmt = "Failed to connect to host: {}", _0)]
    Connect(ConnectError),

    /// Error sending request
    Send(io::Error),

    /// Error parsing response
    Response(ParseError),

    /// Http error
    #[display(fmt = "{}", _0)]
    Http(HttpError),

    /// Http2 error
    #[display(fmt = "{}", _0)]
    H2(h2::Error),

    /// Response took too long
    #[display(fmt = "Timeout while waiting for response")]
    Timeout,

    /// Tunnels are not supported for HTTP/2 connection
    #[display(fmt = "Tunnels are not supported for http2 connection")]
    TunnelNotSupported,

    /// Error sending request body
    Body(BoxError),

    /// Other errors that can occur after submitting a request.
    #[display(fmt = "{:?}: {}", _1, _0)]
    Custom(BoxError, Box<dyn fmt::Debug>),
}

impl std::error::Error for SendRequestError {}

/// A set of errors that can occur during freezing a request
#[derive(Debug, Display, From)]
#[non_exhaustive]
pub enum FreezeRequestError {
    /// Invalid URL
    #[display(fmt = "Invalid URL: {}", _0)]
    Url(InvalidUrl),

    /// HTTP error
    #[display(fmt = "{}", _0)]
    Http(HttpError),

    /// Other errors that can occur after submitting a request.
    #[display(fmt = "{:?}: {}", _1, _0)]
    Custom(BoxError, Box<dyn fmt::Debug>),
}

impl std::error::Error for FreezeRequestError {}

impl From<FreezeRequestError> for SendRequestError {
    fn from(err: FreezeRequestError) -> Self {
        match err {
            FreezeRequestError::Url(err) => err.into(),
            FreezeRequestError::Http(err) => err.into(),
            FreezeRequestError::Custom(err, msg) => SendRequestError::Custom(err, msg),
        }
    }
}