async_curl/
error.rs

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
use std::fmt::Debug;

use curl::easy::Handler;
use tokio::sync::{mpsc::error::SendError, oneshot::error::RecvError};

use crate::actor;

/// This the enum of Errors for this crate.
#[derive(Debug)]
pub enum Error<H>
where
    H: Handler + Debug + Send + 'static,
{
    Curl(curl::Error),
    Multi(curl::MultiError),
    TokioRecv(RecvError),
    TokioSend(SendError<actor::Request<H>>),
}

/// This convert RecvError to our customized
/// Error enum for ease of management of
/// different errors from 3rd party crates.
impl<H> From<RecvError> for Error<H>
where
    H: Handler + Debug + Send + 'static,
{
    fn from(err: RecvError) -> Self {
        Error::TokioRecv(err)
    }
}

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

/// This convert curl::Error to our customized
/// Error enum for ease of management of
/// different errors from 3rd party crates.
impl<H> From<curl::Error> for Error<H>
where
    H: Handler + Debug + Send + 'static,
{
    fn from(err: curl::Error) -> Self {
        Error::Curl(err)
    }
}

impl<H> std::fmt::Display for Error<H>
where
    H: Handler + Debug + Send + 'static,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::Curl(err) => write!(f, "{}", err),
            Error::Multi(err) => write!(f, "{}", err),
            Error::TokioRecv(err) => write!(f, "{}", err),
            Error::TokioSend(err) => write!(f, "{}", err),
        }
    }
}

impl<H> std::error::Error for Error<H> where H: Handler + Debug + Send + 'static {}