forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
185 lines (166 loc) · 5.73 KB
/
Copy patherror.rs
File metadata and controls
185 lines (166 loc) · 5.73 KB
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
pub use ugh_privacy::DbError;
use byteorder;
use openssl::ssl::error::SslError;
use phf;
use std::error;
use std::fmt;
use std::io;
use Result;
use types::Type;
include!(concat!(env!("OUT_DIR"), "/sqlstate.rs"));
/// Reasons a new Postgres connection could fail
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ConnectError {
/// The provided URL could not be parsed
InvalidUrl(String),
/// The URL was missing a user
MissingUser,
/// An error from the Postgres server itself
DbError(DbError),
/// A password was required but not provided in the URL
MissingPassword,
/// The Postgres server requested an authentication method not supported
/// by the driver
UnsupportedAuthentication,
/// The Postgres server does not support SSL encryption
NoSslSupport,
/// There was an error initializing the SSL session
SslError(SslError),
/// There was an error communicating with the server
IoError(io::Error),
/// The server sent an unexpected response
BadResponse,
}
impl fmt::Display for ConnectError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(fmt.write_str(error::Error::description(self)));
match *self {
ConnectError::InvalidUrl(ref msg) => write!(fmt, ": {}", msg),
_ => Ok(())
}
}
}
impl error::Error for ConnectError {
fn description(&self) -> &str {
match *self {
ConnectError::InvalidUrl(_) => "Invalid URL",
ConnectError::MissingUser => "User missing in URL",
ConnectError::DbError(_) => "An error from the Postgres server itself",
ConnectError::MissingPassword => "The server requested a password but none was provided",
ConnectError::UnsupportedAuthentication => {
"The server requested an unsupported authentication method"
}
ConnectError::NoSslSupport => "The server does not support SSL",
ConnectError::SslError(_) => "Error initiating SSL session",
ConnectError::IoError(_) => "Error communicating with server",
ConnectError::BadResponse => "The server returned an unexpected response",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
ConnectError::DbError(ref err) => Some(err),
ConnectError::SslError(ref err) => Some(err),
ConnectError::IoError(ref err) => Some(err),
_ => None
}
}
}
impl error::FromError<io::Error> for ConnectError {
fn from_error(err: io::Error) -> ConnectError {
ConnectError::IoError(err)
}
}
impl error::FromError<DbError> for ConnectError {
fn from_error(err: DbError) -> ConnectError {
ConnectError::DbError(err)
}
}
impl error::FromError<SslError> for ConnectError {
fn from_error(err: SslError) -> ConnectError {
ConnectError::SslError(err)
}
}
impl error::FromError<byteorder::Error> for ConnectError {
fn from_error(err: byteorder::Error) -> ConnectError {
ConnectError::IoError(error::FromError::from_error(err))
}
}
/// Represents the position of an error in a query
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ErrorPosition {
/// A position in the original query
Normal(u32),
/// A position in an internally generated query
Internal {
/// The byte position
position: u32,
/// A query generated by the Postgres server
query: String
}
}
/// An error encountered when communicating with the Postgres server
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Error {
/// An error reported by the Postgres server
DbError(DbError),
/// An error communicating with the Postgres server
IoError(io::Error),
/// The communication channel with the Postgres server has desynchronized
/// due to an earlier communications error.
StreamDesynchronized,
/// An attempt was made to convert between incompatible Rust and Postgres
/// types
WrongType(Type),
/// An attempt was made to read from a column that does not exist
InvalidColumn,
/// A value was NULL but converted to a non-nullable Rust type
WasNull,
/// The server returned an unexpected response
BadResponse,
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(fmt.write_str(error::Error::description(self)));
match *self {
Error::WrongType(ref ty) => write!(fmt, ": saw type {:?}", ty),
_ => Ok(()),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::DbError(_) => "An error reported by the Postgres server",
Error::IoError(_) => "An error communicating with the Postgres server",
Error::StreamDesynchronized => {
"Communication with the server has desynchronized due to an earlier IO error"
}
Error::WrongType(_) => "Unexpected type",
Error::InvalidColumn => "Invalid column",
Error::WasNull => "The value was NULL",
Error::BadResponse => "The server returned an unexpected response",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::DbError(ref err) => Some(err),
Error::IoError(ref err) => Some(err),
_ => None
}
}
}
impl error::FromError<DbError> for Error {
fn from_error(err: DbError) -> Error {
Error::DbError(err)
}
}
impl error::FromError<io::Error> for Error {
fn from_error(err: io::Error) -> Error {
Error::IoError(err)
}
}
impl error::FromError<byteorder::Error> for Error {
fn from_error(err: byteorder::Error) -> Error {
Error::IoError(error::FromError::from_error(err))
}
}