From 3ddcf872036afda8bfd8a0963e4c05bdc31f92ff Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 2 Jan 2016 15:50:11 -0800 Subject: [PATCH] Simplify ConnectError Same deal as Error --- src/error.rs | 29 +++++------------------------ src/lib.rs | 25 +++++++++++++++++++------ src/priv_io.rs | 4 +++- tests/test.rs | 4 ++-- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/error.rs b/src/error.rs index af0cb063..458d0d44 100644 --- a/src/error.rs +++ b/src/error.rs @@ -174,19 +174,10 @@ impl error::Error for DbError { /// Reasons a new Postgres connection could fail. #[derive(Debug)] pub enum ConnectError { - /// An error creating `ConnectParams`. - BadConnectParams(Box), - /// The `ConnectParams` was missing a user. - MissingUser, + /// An error relating to connection parameters. + ConnectParams(Box), /// An error from the Postgres server itself. Db(Box), - /// A password was required but not provided in the `ConnectParams`. - MissingPassword, - /// The Postgres server requested an authentication method not supported - /// by the driver. - UnsupportedAuthentication, - /// The Postgres server does not support SSL encryption. - NoSslSupport, /// An error initializing the SSL session. Ssl(Box), /// An error communicating with the server. @@ -197,11 +188,10 @@ 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::BadConnectParams(ref msg) => write!(fmt, ": {}", msg), + ConnectError::ConnectParams(ref msg) => write!(fmt, ": {}", msg), ConnectError::Db(ref err) => write!(fmt, ": {}", err), ConnectError::Ssl(ref err) => write!(fmt, ": {}", err), ConnectError::Io(ref err) => write!(fmt, ": {}", err), - _ => Ok(()), } } } @@ -209,16 +199,8 @@ impl fmt::Display for ConnectError { impl error::Error for ConnectError { fn description(&self) -> &str { match *self { - ConnectError::BadConnectParams(_) => "Error creating `ConnectParams`", - ConnectError::MissingUser => "User missing in `ConnectParams`", + ConnectError::ConnectParams(_) => "Invalid connection parameters", ConnectError::Db(_) => "Error reported by Postgres", - 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::Ssl(_) => "Error initiating SSL session", ConnectError::Io(_) => "Error communicating with the server", } @@ -226,11 +208,10 @@ impl error::Error for ConnectError { fn cause(&self) -> Option<&error::Error> { match *self { - ConnectError::BadConnectParams(ref err) => Some(&**err), + ConnectError::ConnectParams(ref err) => Some(&**err), ConnectError::Db(ref err) => Some(&**err), ConnectError::Ssl(ref err) => Some(&**err), ConnectError::Io(ref err) => Some(err), - _ => None, } } } diff --git a/src/lib.rs b/src/lib.rs index 4773c7f1..2c7f7bb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -272,7 +272,7 @@ pub fn cancel_query(params: T, -> result::Result<(), ConnectError> where T: IntoConnectParams { - let params = try!(params.into_connect_params().map_err(ConnectError::BadConnectParams)); + let params = try!(params.into_connect_params().map_err(ConnectError::ConnectParams)); let mut socket = try!(priv_io::initialize_stream(¶ms, ssl)); try!(socket.write_message(&CancelRequest { @@ -394,12 +394,18 @@ impl InnerConnection { fn connect(params: T, ssl: SslMode) -> result::Result where T: IntoConnectParams { - let params = try!(params.into_connect_params().map_err(ConnectError::BadConnectParams)); + let params = try!(params.into_connect_params().map_err(ConnectError::ConnectParams)); let stream = try!(priv_io::initialize_stream(¶ms, ssl)); let ConnectParams { user, database, mut options, .. } = params; - let user = try!(user.ok_or(ConnectError::MissingUser)); + let user = match user { + Some(user) => user, + None => { + let err: Box = "User missing from connection parameters".into(); + return Err(ConnectError::ConnectParams(err)); + } + }; let mut conn = InnerConnection { stream: BufStream::new(stream), @@ -564,11 +570,15 @@ impl InnerConnection { match try!(self.read_message()) { AuthenticationOk => return Ok(()), AuthenticationCleartextPassword => { - let pass = try!(user.password.ok_or(ConnectError::MissingPassword)); + let pass = try!(user.password.ok_or_else(|| { + ConnectError::ConnectParams("a password was requested but not provided".into()) + })); try!(self.write_messages(&[PasswordMessage { password: &pass }])); } AuthenticationMD5Password { salt } => { - let pass = try!(user.password.ok_or(ConnectError::MissingPassword)); + let pass = try!(user.password.ok_or_else(|| { + ConnectError::ConnectParams("a password was requested but not provided".into()) + })); let mut hasher = Md5::new(); hasher.input(pass.as_bytes()); hasher.input(user.user.as_bytes()); @@ -582,7 +592,10 @@ impl InnerConnection { AuthenticationKerberosV5 | AuthenticationSCMCredential | AuthenticationGSS | - AuthenticationSSPI => return Err(ConnectError::UnsupportedAuthentication), + AuthenticationSSPI => { + return Err(ConnectError::Io(std_io::Error::new(std_io::ErrorKind::Other, + "unsupported authentication"))) + } ErrorResponse { fields } => return DbError::new_connect(fields), _ => return Err(ConnectError::Io(bad_response())), } diff --git a/src/priv_io.rs b/src/priv_io.rs index 2f4bb813..e4af4d60 100644 --- a/src/priv_io.rs +++ b/src/priv_io.rs @@ -1,5 +1,6 @@ use byteorder::ReadBytesExt; use net2::TcpStreamExt; +use std::error::Error; use std::io; use std::io::prelude::*; use std::fmt; @@ -172,7 +173,8 @@ pub fn initialize_stream(params: &ConnectParams, if try!(socket.read_u8()) == b'N' { if ssl_required { - return Err(ConnectError::NoSslSupport); + let err: Box = "The server does not support SSL".into(); + return Err(ConnectError::Ssl(err)); } else { return Ok(Box::new(socket)); } diff --git a/tests/test.rs b/tests/test.rs index e7a61b1c..79900443 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -698,7 +698,7 @@ fn test_plaintext_pass() { fn test_plaintext_pass_no_pass() { let ret = Connection::connect("postgres://pass_user@localhost/postgres", SslMode::None); match ret { - Err(ConnectError::MissingPassword) => (), + Err(ConnectError::ConnectParams(..)) => (), Err(err) => panic!("Unexpected error {:?}", err), _ => panic!("Expected error") } @@ -723,7 +723,7 @@ fn test_md5_pass() { fn test_md5_pass_no_pass() { let ret = Connection::connect("postgres://md5_user@localhost/postgres", SslMode::None); match ret { - Err(ConnectError::MissingPassword) => (), + Err(ConnectError::ConnectParams(..)) => (), Err(err) => panic!("Unexpected error {:?}", err), _ => panic!("Expected error") }