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
use std::net::SocketAddr;
use std::sync::Arc;
use futures::task::SpawnError;
use thiserror::Error;
use tor_error::{internal, ErrorKind};
use tor_proto::ClockSkew;
#[derive(Debug, Error, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("Target was unusable: {0}")]
UnusableTarget(#[source] tor_error::Bug),
#[error("Pending channel failed to launch")]
PendingFailed,
#[error("Channel timed out")]
ChanTimeout,
#[error("Protocol error while opening a channel.")]
Proto {
#[source]
source: tor_proto::Error,
clock_skew: Option<ClockSkew>,
},
#[error("Network IO error, or TLS error, in {action}, talking to {peer}")]
Io {
peer: SocketAddr,
action: &'static str,
#[source]
source: Arc<std::io::Error>,
},
#[error("Channel build failed: [(address, error)] = {addresses:?}")]
ChannelBuild {
addresses: Vec<(SocketAddr, Arc<std::io::Error>)>,
},
#[error("unable to spawn {spawning}")]
Spawn {
spawning: &'static str,
#[source]
cause: Arc<SpawnError>,
},
#[error("Internal error: {0}")]
Internal(#[from] tor_error::Bug),
}
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(internal!("Thread failed while holding lock"))
}
}
impl tor_error::HasKind for Error {
fn kind(&self) -> ErrorKind {
use tor_proto::Error as ProtoErr;
use Error as E;
use ErrorKind as EK;
match self {
E::ChanTimeout
| E::Io { .. }
| E::Proto {
source: ProtoErr::ChanIoErr(_),
..
} => EK::TorAccessFailed,
E::Spawn { cause, .. } => cause.kind(),
E::Proto { source, .. } => source.kind(),
E::PendingFailed => EK::TorAccessFailed,
E::UnusableTarget(_) | E::Internal(_) => EK::Internal,
Error::ChannelBuild { .. } => EK::TorAccessFailed,
}
}
}
impl tor_error::HasRetryTime for Error {
fn retry_time(&self) -> tor_error::RetryTime {
use tor_error::RetryTime as RT;
use Error as E;
match self {
E::ChanTimeout => RT::Immediate,
E::PendingFailed | E::Proto { .. } | E::Io { .. } => RT::AfterWaiting,
E::ChannelBuild { .. } => RT::AfterWaiting,
E::UnusableTarget(_) => RT::Never,
E::Spawn { .. } | E::Internal(_) => RT::Never,
}
}
}
impl Error {
pub(crate) fn from_spawn(spawning: &'static str, err: SpawnError) -> Error {
Error::Spawn {
spawning,
cause: Arc::new(err),
}
}
pub(crate) fn from_proto_no_skew(source: tor_proto::Error) -> Self {
Error::Proto {
source,
clock_skew: None,
}
}
pub fn clock_skew(&self) -> Option<ClockSkew> {
match self {
Error::Proto { clock_skew, .. } => *clock_skew,
_ => None,
}
}
}