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
use std::{fmt, io};
use futures::channel::mpsc;
use thiserror::Error;
use trust_dns_proto::error::{ProtoError, ProtoErrorKind};
use crate::error::{DnsSecError, DnsSecErrorKind};
use crate::proto::{trace, ExtBacktrace};
pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum ErrorKind {
#[error("{0}")]
Message(&'static str),
#[error("{0}")]
Msg(String),
#[error("dnssec error")]
DnsSec(#[from] DnsSecError),
#[error("io error")]
Io(#[from] std::io::Error),
#[error("proto error")]
Proto(#[from] ProtoError),
#[error("error sending to mpsc: {0}")]
SendError(#[from] mpsc::SendError),
#[error("request timed out")]
Timeout,
}
impl Clone for ErrorKind {
fn clone(&self) -> Self {
use self::ErrorKind::*;
match self {
Message(msg) => Message(msg),
Msg(ref msg) => Msg(msg.clone()),
DnsSec(dnssec) => DnsSec(dnssec.clone()),
Io(io) => Io(std::io::Error::from(io.kind())),
Proto(proto) => Proto(proto.clone()),
SendError(e) => SendError(e.clone()),
Timeout => Timeout,
}
}
}
#[derive(Debug, Error, Clone)]
pub struct Error {
kind: ErrorKind,
backtrack: Option<ExtBacktrace>,
}
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref backtrace) = self.backtrack {
fmt::Display::fmt(&self.kind, f)?;
fmt::Debug::fmt(backtrace, f)
} else {
fmt::Display::fmt(&self.kind, f)
}
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
kind,
backtrack: trace!(),
}
}
}
impl From<&'static str> for Error {
fn from(msg: &'static str) -> Error {
ErrorKind::Message(msg).into()
}
}
impl From<mpsc::SendError> for Error {
fn from(e: mpsc::SendError) -> Self {
ErrorKind::from(e).into()
}
}
impl From<String> for Error {
fn from(msg: String) -> Error {
ErrorKind::Msg(msg).into()
}
}
impl From<DnsSecError> for Error {
fn from(e: DnsSecError) -> Error {
match *e.kind() {
DnsSecErrorKind::Timeout => ErrorKind::Timeout.into(),
_ => ErrorKind::from(e).into(),
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
match e.kind() {
io::ErrorKind::TimedOut => ErrorKind::Timeout.into(),
_ => ErrorKind::from(e).into(),
}
}
}
impl From<ProtoError> for Error {
fn from(e: ProtoError) -> Error {
match *e.kind() {
ProtoErrorKind::Timeout => ErrorKind::Timeout.into(),
_ => ErrorKind::from(e).into(),
}
}
}
impl From<Error> for io::Error {
fn from(e: Error) -> Self {
match *e.kind() {
ErrorKind::Timeout => io::Error::new(io::ErrorKind::TimedOut, e),
_ => io::Error::new(io::ErrorKind::Other, e),
}
}
}
#[test]
fn test_conversion() {
let io_error = io::Error::new(io::ErrorKind::TimedOut, "mock timeout");
let error = Error::from(io_error);
match *error.kind() {
ErrorKind::Timeout => (),
_ => panic!("incorrect type: {}", error),
}
}