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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::{borrow::Cow, io, mem, str::Utf8Error, string::FromUtf8Error};
use failure::*;
use tokio::prelude::*;
use tokio_timer::timeout::Error as TimeoutError;
use tokio_timer::Error as TimerError;
use url::ParseError;
use crate::types::Packet;
#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "Driver error: `{}`", _0)]
Driver(#[cause] DriverError),
#[fail(display = "Input/output error: `{}`", _0)]
Io(#[cause] io::Error),
#[fail(display = "Other error: `{}`", _0)]
Other(#[cause] failure::Error),
#[fail(display = "Server error: `{}`", _0)]
Server(#[cause] ServerError),
#[fail(display = "URL error: `{}`", _0)]
Url(#[cause] UrlError),
#[fail(display = "From SQL error: `{}`", _0)]
FromSql(#[cause] FromSqlError),
}
#[derive(Debug, Fail, Clone)]
#[fail(display = "ERROR {} ({}): {}", name, code, message)]
pub struct ServerError {
pub code: u32,
pub name: String,
pub message: String,
pub stack_trace: String,
}
#[derive(Debug, Fail)]
pub enum UrlError {
#[fail(display = "Invalid or incomplete connection URL")]
Invalid,
#[fail(
display = "Invalid value `{}' for connection URL parameter `{}'",
value, param
)]
InvalidParamValue { param: String, value: String },
#[fail(display = "URL parse error: {}", _0)]
Parse(#[cause] ParseError),
#[fail(display = "Unknown connection URL parameter `{}'", param)]
UnknownParameter { param: String },
#[fail(display = "Unsupported connection URL scheme `{}'", scheme)]
UnsupportedScheme { scheme: String },
}
#[derive(Debug, Fail)]
pub enum DriverError {
#[fail(display = "Varint overflows a 64-bit integer.")]
Overflow,
#[fail(display = "Unknown packet 0x{:x}.", packet)]
UnknownPacket { packet: u64 },
#[fail(display = "Unexpected packet.")]
UnexpectedPacket,
#[fail(display = "Timeout error.")]
Timeout,
#[fail(display = "Invalid utf-8 sequence.")]
Utf8Error(Utf8Error),
}
#[derive(Debug, Fail)]
pub enum FromSqlError {
#[fail(display = "SqlType::{} cannot be cast to {}.", src, dst)]
InvalidType {
src: Cow<'static, str>,
dst: Cow<'static, str>,
},
#[fail(display = "Out of range.")]
OutOfRange,
}
impl From<DriverError> for Error {
fn from(err: DriverError) -> Self {
Error::Driver(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
impl From<ServerError> for Error {
fn from(err: ServerError) -> Self {
Error::Server(err)
}
}
impl From<UrlError> for Error {
fn from(err: UrlError) -> Self {
Error::Url(err)
}
}
impl From<String> for Error {
fn from(err: String) -> Self {
Error::Other(failure::Context::new(err).into())
}
}
impl From<&str> for Error {
fn from(err: &str) -> Self {
Error::Other(failure::Context::new(err.to_string()).into())
}
}
impl From<FromUtf8Error> for Error {
fn from(err: FromUtf8Error) -> Self {
Error::Other(failure::Context::new(err).into())
}
}
impl From<TimerError> for Error {
fn from(err: TimerError) -> Self {
Error::Other(failure::Context::new(err).into())
}
}
impl From<ParseError> for Error {
fn from(err: ParseError) -> Self {
Error::Url(UrlError::Parse(err))
}
}
impl From<TimeoutError<Error>> for Error {
fn from(err: TimeoutError<Self>) -> Self {
match err.into_inner() {
None => Error::Driver(DriverError::Timeout),
Some(inner) => inner,
}
}
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Self {
Error::Driver(DriverError::Utf8Error(err))
}
}
impl<S> Into<Poll<Option<Packet<S>>, Error>> for Error {
fn into(self) -> Poll<Option<Packet<S>>, Error> {
let mut this = self;
if let Error::Io(ref mut e) = &mut this {
if e.kind() == io::ErrorKind::WouldBlock {
return Ok(Async::NotReady);
}
let me = mem::replace(e, io::Error::from(io::ErrorKind::Other));
return Err(Error::Io(me));
}
warn!("ERROR: {:?}", this);
Err(this)
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> Self {
match err {
Error::Io(error) => error,
e => io::Error::new(io::ErrorKind::Other, e.to_string()),
}
}
}