summaryrefslogtreecommitdiff
path: root/tor-chanmgr/src/err.rs
blob: 15759cc2a8d64cf42187696bd36c69db2f3be0d0 (plain)
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
//! Declare error types for tor-chanmgr

use std::sync::Arc;
use thiserror::Error;

/// An error returned by a channel manager.
#[derive(Debug, Error, Clone)]
#[non_exhaustive]
pub enum Error {
    /// A ChanTarget was given for which no channel could be built.
    #[error("Target was unusable: {0}")]
    UnusableTarget(String),

    /// We were waiting on a pending channel, but it didn't succeed.
    #[error("Pending channel failed to launch")]
    PendingFailed,

    /// It took too long for us to establish this connection.
    #[error("Channel timed out")]
    ChanTimeout,

    /// A protocol error while making a channel
    #[error("Protocol error while opening a channel: {0}")]
    Proto(#[from] tor_proto::Error),

    /// A protocol error while making a channel
    #[error("I/O error while opening a channel: {0}")]
    Io(#[source] Arc<std::io::Error>),

    /// An internal error of some kind that should never occur.
    #[error("Internal error: {0}")]
    Internal(&'static str),

    /// We were waiting for a channel to complete, but it failed.
    #[error("Pending channel failed to open: {0}")]
    PendingChanFailed(#[from] PendingChanError),
}

impl From<futures::task::SpawnError> for Error {
    fn from(_: futures::task::SpawnError) -> Error {
        Error::Internal("Couldn't spawn channel reactor")
    }
}

impl From<tor_rtcompat::TimeoutError> for Error {
    fn from(_: tor_rtcompat::TimeoutError) -> Error {
        Error::ChanTimeout
    }
}

impl<T> From<std::sync::PoisonError<T>> for Error {
    fn from(_: std::sync::PoisonError<T>) -> Error {
        Error::Internal("Thread failed while holding lock")
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Error {
        Error::Io(Arc::new(e))
    }
}

/// An error transmitted by a future that trying to build a channel.
#[derive(Debug, Clone)]
pub struct PendingChanError(String);
impl std::error::Error for PendingChanError {}
impl std::fmt::Display for PendingChanError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl From<&Error> for PendingChanError {
    fn from(e: &Error) -> PendingChanError {
        PendingChanError(e.to_string())
    }
}