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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Types for error handling.

use std::{error::Error as StdError, fmt, io, sync::Arc};

/// A non-exhaustive list of error types that can occur while sending an HTTP
/// request or receiving an HTTP response.
///
/// These are meant to be treated as general error codes that allow you to
/// handle different sorts of errors in different ways, but are not always
/// specific. The list is also non-exhaustive, and more variants may be added in
/// the future.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind {
    /// A problem occurred with the local certificate.
    BadClientCertificate,

    /// The server certificate could not be validated.
    BadServerCertificate,

    /// The HTTP client failed to initialize.
    ClientInitialization,

    /// Failed to connect to the server.
    ConnectionFailed,

    /// The server either returned a response using an unknown or unsupported
    /// encoding format, or the response encoding was malformed.
    InvalidContentEncoding,

    /// Provided authentication credentials were rejected by the server.
    ///
    /// This error is only returned when using Isahc's built-in authentication
    /// methods. If using authentication headers manually, the server's response
    /// will be returned as a success unaltered.
    InvalidCredentials,

    /// The request to be sent was invalid and could not be sent.
    ///
    /// Note that this is only returned for requests that the client deemed
    /// invalid. If the request appears to be valid but is rejected by the
    /// server, then the server's response will likely indicate as such.
    InvalidRequest,

    /// An I/O error either sending the request or reading the response. This
    /// could be caused by a problem on the client machine, a problem on the
    /// server machine, or a problem with the network between the two.
    Io,

    /// Failed to resolve a host name.
    ///
    /// This could be caused by any number of problems, including failure to
    /// reach a DNS server, misconfigured resolver configuration, or the
    /// hostname simply does not exist.
    NameResolution,

    /// The server made an unrecoverable HTTP protocol violation. This indicates
    /// a bug in the server. Retrying a request that returns this error is
    /// likely to produce the same error.
    ProtocolViolation,

    /// Request processing could not continue because the client needed to
    /// re-send the request body, but was unable to rewind the body stream to
    /// the beginning in order to do so.
    RequestBodyNotRewindable,

    /// A request or operation took longer than the configured timeout time.
    Timeout,

    /// An error ocurred in the secure socket engine.
    TlsEngine,

    /// Number of redirects hit the maximum amount.
    TooManyRedirects,

    /// An unknown error occurred. This likely indicates a problem in the HTTP
    /// client or in a dependency, but the client was able to recover instead of
    /// panicking. Subsequent requests will likely succeed.
    ///
    /// Only used internally.
    #[doc(hidden)]
    Unknown,
}

impl ErrorKind {
    #[inline]
    fn description(&self) -> Option<&str> {
        match self {
            Self::BadClientCertificate => Some("a problem occurred with the local certificate"),
            Self::BadServerCertificate => Some("the server certificate could not be validated"),
            Self::ClientInitialization => Some("failed to initialize client"),
            Self::ConnectionFailed => Some("failed to connect to the server"),
            Self::InvalidContentEncoding => Some(
                "the server either returned a response using an unknown or unsupported encoding format, or the response encoding was malformed",
            ),
            Self::InvalidCredentials => {
                Some("provided authentication credentials were rejected by the server")
            }
            Self::InvalidRequest => Some("invalid HTTP request"),
            Self::NameResolution => Some("failed to resolve host name"),
            Self::ProtocolViolation => {
                Some("the server made an unrecoverable HTTP protocol violation")
            }
            Self::RequestBodyNotRewindable => {
                Some("request body could not be re-sent because it is not rewindable")
            }
            Self::Timeout => {
                Some("request or operation took longer than the configured timeout time")
            }
            Self::TlsEngine => Some("error ocurred in the secure socket engine"),
            Self::TooManyRedirects => Some("number of redirects hit the maximum amount"),
            _ => None,
        }
    }
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.description().unwrap_or("unknown error"))
    }
}

// Improve equality ergonomics for references.
impl PartialEq<ErrorKind> for &'_ ErrorKind {
    fn eq(&self, other: &ErrorKind) -> bool {
        *self == other
    }
}

/// An error encountered while sending an HTTP request or receiving an HTTP
/// response.
///
/// This type is intentionally opaque, as sending an HTTP request involves many
/// different moving parts, some of which can be platform or device-dependent.
/// It is recommended that you use the [`kind`][Error::kind] method to get a
/// more generalized classification of error types that this error could be if
/// you need to handle different sorts of errors in different ways.
///
/// If you need to get more specific details about the reason for the error, you
/// can use the [`source`][std::error::Error::source] method. We do not provide
/// any stability guarantees about what error sources are returned.
#[derive(Clone)]
pub struct Error(Arc<Inner>);

struct Inner {
    kind: ErrorKind,
    context: Option<String>,
    source: Option<Box<dyn StdError + Send + Sync>>,
}

impl Error {
    /// Create a new error from a given error kind and source error.
    pub(crate) fn new<E>(kind: ErrorKind, source: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::with_context(kind, None, source)
    }

    /// Create a new error from a given error kind, source error, and context
    /// string.
    pub(crate) fn with_context<E>(kind: ErrorKind, context: Option<String>, source: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self(Arc::new(Inner {
            kind,
            context,
            source: Some(Box::new(source)),
        }))
    }

    /// Statically cast a given error into an Isahc error, converting if
    /// necessary.
    ///
    /// This is useful for converting or creating errors from external types
    /// without publicly implementing `From` over them and leaking them into our
    /// API.
    pub(crate) fn from_any<E>(error: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        match_type! {
            <error as Error> => error,
            <error as std::io::Error> => error.into(),
            <error as curl::Error> => {
                Self::with_context(
                    if error.is_ssl_certproblem() || error.is_ssl_cacert_badfile() {
                        ErrorKind::BadClientCertificate
                    } else if error.is_peer_failed_verification()
                        || error.is_ssl_cacert()
                        || error.is_ssl_cipher()
                        || error.is_ssl_issuer_error()
                    {
                        ErrorKind::BadServerCertificate
                    } else if error.is_interface_failed() {
                        ErrorKind::ClientInitialization
                    } else if error.is_couldnt_connect() || error.is_ssl_connect_error() {
                        ErrorKind::ConnectionFailed
                    } else if error.is_bad_content_encoding() || error.is_conv_failed() {
                        ErrorKind::InvalidContentEncoding
                    } else if error.is_login_denied() {
                        ErrorKind::InvalidCredentials
                    } else if error.is_url_malformed() {
                        ErrorKind::InvalidRequest
                    } else if error.is_couldnt_resolve_host() || error.is_couldnt_resolve_proxy() {
                        ErrorKind::NameResolution
                    } else if error.is_got_nothing()
                        || error.is_http2_error()
                        || error.is_http2_stream_error()
                        || error.is_unsupported_protocol()
                        || error.code() == curl_sys::CURLE_FTP_WEIRD_SERVER_REPLY
                    {
                        ErrorKind::ProtocolViolation
                    } else if error.is_send_error()
                        || error.is_recv_error()
                        || error.is_read_error()
                        || error.is_write_error()
                        || error.is_upload_failed()
                        || error.is_send_fail_rewind()
                        || error.is_aborted_by_callback()
                        || error.is_partial_file()
                    {
                        ErrorKind::Io
                    } else if error.is_ssl_engine_initfailed()
                        || error.is_ssl_engine_notfound()
                        || error.is_ssl_engine_setfailed()
                    {
                        ErrorKind::TlsEngine
                    } else if error.is_operation_timedout() {
                        ErrorKind::Timeout
                    } else if error.is_too_many_redirects() {
                        ErrorKind::TooManyRedirects
                    } else {
                        ErrorKind::Unknown
                    },
                    error.extra_description().map(String::from),
                    error,
                )
            },
            <error as curl::MultiError> => {
                Self::new(
                    if error.is_bad_socket() {
                        ErrorKind::Io
                    } else {
                        ErrorKind::Unknown
                    },
                    error,
                )
            },
            error => Error::new(ErrorKind::Unknown, error),
        }
    }

    /// Get the kind of error this represents.
    ///
    /// The kind returned may not be matchable against any known documented if
    /// the reason for the error is unknown. Unknown errors may be an indication
    /// of a bug, or an error condition that we do not recognize appropriately.
    /// Either way, please report such occurrences to us!
    #[inline]
    pub fn kind(&self) -> &ErrorKind {
        &self.0.kind
    }

    /// Returns true if this error was likely caused by the client.
    ///
    /// Usually indicates that the client was misconfigured or used to send
    /// invalid data to the server. Requests that return these sorts of errors
    /// probably should not be retried without first fixing the request
    /// parameters.
    pub fn is_client(&self) -> bool {
        match self.kind() {
            ErrorKind::BadClientCertificate
            | ErrorKind::ClientInitialization
            | ErrorKind::InvalidCredentials
            | ErrorKind::InvalidRequest
            | ErrorKind::RequestBodyNotRewindable
            | ErrorKind::TlsEngine => true,
            _ => false,
        }
    }

    /// Returns true if this is an error likely related to network failures.
    pub fn is_network(&self) -> bool {
        match self.kind() {
            ErrorKind::ConnectionFailed | ErrorKind::Io | ErrorKind::NameResolution => true,
            _ => false,
        }
    }

    /// Returns true if this error was likely the fault of the server.
    pub fn is_server(&self) -> bool {
        match self.kind() {
            ErrorKind::BadServerCertificate
            | ErrorKind::ProtocolViolation
            | ErrorKind::TooManyRedirects => true,
            _ => false,
        }
    }

    /// Returns true if this error is related to SSL/TLS.
    pub fn is_tls(&self) -> bool {
        match self.kind() {
            ErrorKind::BadClientCertificate
            | ErrorKind::BadServerCertificate
            | ErrorKind::TlsEngine => true,
            _ => false,
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        self.0.source.as_ref().map(|source| &**source as _)
    }
}

impl PartialEq<ErrorKind> for Error {
    fn eq(&self, kind: &ErrorKind) -> bool {
        self.kind().eq(kind)
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Error")
            .field("kind", &self.kind())
            .field("context", &self.0.context)
            .field("source", &self.source())
            .finish()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(s) = self.0.context.as_ref() {
            write!(f, "{}: {}", self.kind(), s)
        } else {
            write!(f, "{}", self.kind())
        }
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Self(Arc::new(Inner {
            kind,
            context: None,
            source: None,
        }))
    }
}

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        // If this I/O error is just a wrapped Isahc error, then unwrap it.
        if let Some(inner) = error.get_ref() {
            if inner.is::<Self>() {
                return *error.into_inner().unwrap().downcast().unwrap();
            }
        }

        Self::new(
            match error.kind() {
                io::ErrorKind::ConnectionRefused => ErrorKind::ConnectionFailed,
                io::ErrorKind::TimedOut => ErrorKind::Timeout,
                _ => ErrorKind::Io,
            },
            error,
        )
    }
}

impl From<Error> for io::Error {
    fn from(error: Error) -> Self {
        let kind = match error.kind() {
            ErrorKind::ConnectionFailed => io::ErrorKind::ConnectionRefused,
            ErrorKind::Timeout => io::ErrorKind::TimedOut,
            _ => io::ErrorKind::Other,
        };

        Self::new(kind, error)
    }
}

impl From<http::Error> for Error {
    fn from(error: http::Error) -> Error {
        Self::new(
            if error.is::<http::header::InvalidHeaderName>()
                || error.is::<http::header::InvalidHeaderValue>()
                || error.is::<http::method::InvalidMethod>()
                || error.is::<http::uri::InvalidUri>()
                || error.is::<http::uri::InvalidUriParts>()
            {
                ErrorKind::InvalidRequest
            } else {
                ErrorKind::Unknown
            },
            error,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    static_assertions::assert_impl_all!(Error: Send, Sync);
}