Skip to content

Commit 3ddcf87

Browse files
committed
Simplify ConnectError
Same deal as Error
1 parent ccfba62 commit 3ddcf87

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

src/error.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,10 @@ impl error::Error for DbError {
174174
/// Reasons a new Postgres connection could fail.
175175
#[derive(Debug)]
176176
pub enum ConnectError {
177-
/// An error creating `ConnectParams`.
178-
BadConnectParams(Box<error::Error + Sync + Send>),
179-
/// The `ConnectParams` was missing a user.
180-
MissingUser,
177+
/// An error relating to connection parameters.
178+
ConnectParams(Box<error::Error + Sync + Send>),
181179
/// An error from the Postgres server itself.
182180
Db(Box<DbError>),
183-
/// A password was required but not provided in the `ConnectParams`.
184-
MissingPassword,
185-
/// The Postgres server requested an authentication method not supported
186-
/// by the driver.
187-
UnsupportedAuthentication,
188-
/// The Postgres server does not support SSL encryption.
189-
NoSslSupport,
190181
/// An error initializing the SSL session.
191182
Ssl(Box<error::Error + Sync + Send>),
192183
/// An error communicating with the server.
@@ -197,40 +188,30 @@ impl fmt::Display for ConnectError {
197188
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
198189
try!(fmt.write_str(error::Error::description(self)));
199190
match *self {
200-
ConnectError::BadConnectParams(ref msg) => write!(fmt, ": {}", msg),
191+
ConnectError::ConnectParams(ref msg) => write!(fmt, ": {}", msg),
201192
ConnectError::Db(ref err) => write!(fmt, ": {}", err),
202193
ConnectError::Ssl(ref err) => write!(fmt, ": {}", err),
203194
ConnectError::Io(ref err) => write!(fmt, ": {}", err),
204-
_ => Ok(()),
205195
}
206196
}
207197
}
208198

209199
impl error::Error for ConnectError {
210200
fn description(&self) -> &str {
211201
match *self {
212-
ConnectError::BadConnectParams(_) => "Error creating `ConnectParams`",
213-
ConnectError::MissingUser => "User missing in `ConnectParams`",
202+
ConnectError::ConnectParams(_) => "Invalid connection parameters",
214203
ConnectError::Db(_) => "Error reported by Postgres",
215-
ConnectError::MissingPassword => {
216-
"The server requested a password but none was provided"
217-
}
218-
ConnectError::UnsupportedAuthentication => {
219-
"The server requested an unsupported authentication method"
220-
}
221-
ConnectError::NoSslSupport => "The server does not support SSL",
222204
ConnectError::Ssl(_) => "Error initiating SSL session",
223205
ConnectError::Io(_) => "Error communicating with the server",
224206
}
225207
}
226208

227209
fn cause(&self) -> Option<&error::Error> {
228210
match *self {
229-
ConnectError::BadConnectParams(ref err) => Some(&**err),
211+
ConnectError::ConnectParams(ref err) => Some(&**err),
230212
ConnectError::Db(ref err) => Some(&**err),
231213
ConnectError::Ssl(ref err) => Some(&**err),
232214
ConnectError::Io(ref err) => Some(err),
233-
_ => None,
234215
}
235216
}
236217
}

src/lib.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub fn cancel_query<T>(params: T,
272272
-> result::Result<(), ConnectError>
273273
where T: IntoConnectParams
274274
{
275-
let params = try!(params.into_connect_params().map_err(ConnectError::BadConnectParams));
275+
let params = try!(params.into_connect_params().map_err(ConnectError::ConnectParams));
276276
let mut socket = try!(priv_io::initialize_stream(&params, ssl));
277277

278278
try!(socket.write_message(&CancelRequest {
@@ -394,12 +394,18 @@ impl InnerConnection {
394394
fn connect<T>(params: T, ssl: SslMode) -> result::Result<InnerConnection, ConnectError>
395395
where T: IntoConnectParams
396396
{
397-
let params = try!(params.into_connect_params().map_err(ConnectError::BadConnectParams));
397+
let params = try!(params.into_connect_params().map_err(ConnectError::ConnectParams));
398398
let stream = try!(priv_io::initialize_stream(&params, ssl));
399399

400400
let ConnectParams { user, database, mut options, .. } = params;
401401

402-
let user = try!(user.ok_or(ConnectError::MissingUser));
402+
let user = match user {
403+
Some(user) => user,
404+
None => {
405+
let err: Box<StdError + StdSync + Send> = "User missing from connection parameters".into();
406+
return Err(ConnectError::ConnectParams(err));
407+
}
408+
};
403409

404410
let mut conn = InnerConnection {
405411
stream: BufStream::new(stream),
@@ -564,11 +570,15 @@ impl InnerConnection {
564570
match try!(self.read_message()) {
565571
AuthenticationOk => return Ok(()),
566572
AuthenticationCleartextPassword => {
567-
let pass = try!(user.password.ok_or(ConnectError::MissingPassword));
573+
let pass = try!(user.password.ok_or_else(|| {
574+
ConnectError::ConnectParams("a password was requested but not provided".into())
575+
}));
568576
try!(self.write_messages(&[PasswordMessage { password: &pass }]));
569577
}
570578
AuthenticationMD5Password { salt } => {
571-
let pass = try!(user.password.ok_or(ConnectError::MissingPassword));
579+
let pass = try!(user.password.ok_or_else(|| {
580+
ConnectError::ConnectParams("a password was requested but not provided".into())
581+
}));
572582
let mut hasher = Md5::new();
573583
hasher.input(pass.as_bytes());
574584
hasher.input(user.user.as_bytes());
@@ -582,7 +592,10 @@ impl InnerConnection {
582592
AuthenticationKerberosV5 |
583593
AuthenticationSCMCredential |
584594
AuthenticationGSS |
585-
AuthenticationSSPI => return Err(ConnectError::UnsupportedAuthentication),
595+
AuthenticationSSPI => {
596+
return Err(ConnectError::Io(std_io::Error::new(std_io::ErrorKind::Other,
597+
"unsupported authentication")))
598+
}
586599
ErrorResponse { fields } => return DbError::new_connect(fields),
587600
_ => return Err(ConnectError::Io(bad_response())),
588601
}

src/priv_io.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use byteorder::ReadBytesExt;
22
use net2::TcpStreamExt;
3+
use std::error::Error;
34
use std::io;
45
use std::io::prelude::*;
56
use std::fmt;
@@ -172,7 +173,8 @@ pub fn initialize_stream(params: &ConnectParams,
172173

173174
if try!(socket.read_u8()) == b'N' {
174175
if ssl_required {
175-
return Err(ConnectError::NoSslSupport);
176+
let err: Box<Error + Sync + Send> = "The server does not support SSL".into();
177+
return Err(ConnectError::Ssl(err));
176178
} else {
177179
return Ok(Box::new(socket));
178180
}

tests/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ fn test_plaintext_pass() {
698698
fn test_plaintext_pass_no_pass() {
699699
let ret = Connection::connect("postgres://pass_user@localhost/postgres", SslMode::None);
700700
match ret {
701-
Err(ConnectError::MissingPassword) => (),
701+
Err(ConnectError::ConnectParams(..)) => (),
702702
Err(err) => panic!("Unexpected error {:?}", err),
703703
_ => panic!("Expected error")
704704
}
@@ -723,7 +723,7 @@ fn test_md5_pass() {
723723
fn test_md5_pass_no_pass() {
724724
let ret = Connection::connect("postgres://md5_user@localhost/postgres", SslMode::None);
725725
match ret {
726-
Err(ConnectError::MissingPassword) => (),
726+
Err(ConnectError::ConnectParams(..)) => (),
727727
Err(err) => panic!("Unexpected error {:?}", err),
728728
_ => panic!("Expected error")
729729
}

0 commit comments

Comments
 (0)