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
use crate::async_curl;
use curl::{easy::Handler, MultiError};
use std::{
    error,
    fmt::{self, Debug},
};
use tokio::sync::{mpsc::error::SendError, oneshot::error::RecvError};

#[derive(Debug)]
pub struct AsyncCurlError(pub String);

/// This convert MultiError to our customized
/// AsyncCurlError for ease of management of
/// different errors from 3rd party crates.
impl From<MultiError> for AsyncCurlError {
    fn from(err: MultiError) -> Self {
        AsyncCurlError(format!("{:?}", err))
    }
}

/// This convert RecvError to our customized
/// AsyncCurlError for ease of management of
/// different errors from 3rd party crates.
impl From<RecvError> for AsyncCurlError {
    fn from(err: RecvError) -> Self {
        AsyncCurlError(format!("{:?}", err))
    }
}

/// This convert SendError to our customized
/// AsyncCurlError for ease of management of
/// different errors from 3rd party crates.
impl<H> From<SendError<async_curl::Request<H>>> for AsyncCurlError
where
    H: Handler + Debug + Send + 'static,
{
    fn from(err: SendError<async_curl::Request<H>>) -> Self {
        AsyncCurlError(format!("{:?}", err))
    }
}

impl From<curl::Error> for AsyncCurlError {
    fn from(err: curl::Error) -> Self {
        AsyncCurlError(format!("{:?}", err))
    }
}

impl fmt::Display for AsyncCurlError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl error::Error for AsyncCurlError {}