Skip to main content

async_curl/
error.rs

1use std::fmt::Debug;
2
3use curl::easy::Handler;
4use tokio::sync::{mpsc::error::SendError, oneshot::error::RecvError};
5
6use crate::actor;
7
8/// This the enum of Errors for this crate.
9#[derive(Debug)]
10pub enum Error<H>
11where
12    H: Handler + Debug + Send + 'static,
13{
14    Curl(curl::Error),
15    Multi(curl::MultiError),
16    TokioRecv(RecvError),
17    TokioSend(SendError<actor::Request<H>>),
18    JoinError(tokio::task::JoinError),
19}
20
21/// This convert RecvError to our customized
22/// Error enum for ease of management of
23/// different errors from 3rd party crates.
24impl<H> From<RecvError> for Error<H>
25where
26    H: Handler + Debug + Send + 'static,
27{
28    fn from(err: RecvError) -> Self {
29        Error::TokioRecv(err)
30    }
31}
32
33/// This convert SendError to our customized
34/// Error enum for ease of management of
35/// different errors from 3rd party crates.
36impl<H> From<SendError<actor::Request<H>>> for Error<H>
37where
38    H: Handler + Debug + Send + 'static,
39{
40    fn from(err: SendError<actor::Request<H>>) -> Self {
41        Error::TokioSend(err)
42    }
43}
44
45/// This convert curl::Error to our customized
46/// Error enum for ease of management of
47/// different errors from 3rd party crates.
48impl<H> From<curl::Error> for Error<H>
49where
50    H: Handler + Debug + Send + 'static,
51{
52    fn from(err: curl::Error) -> Self {
53        Error::Curl(err)
54    }
55}
56
57impl<H> std::fmt::Display for Error<H>
58where
59    H: Handler + Debug + Send + 'static,
60{
61    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
62        match self {
63            Error::Curl(err) => write!(f, "{}", err),
64            Error::Multi(err) => write!(f, "{}", err),
65            Error::TokioRecv(err) => write!(f, "{}", err),
66            Error::TokioSend(err) => write!(f, "{}", err),
67            Error::JoinError(err) => write!(f, "{}", err),
68        }
69    }
70}
71
72impl<H> std::error::Error for Error<H> where H: Handler + Debug + Send + 'static {}