From de8c882ef035a789ff38c05701b033a0993be39f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 15 Jun 2015 22:20:09 -0700 Subject: [PATCH] Generify connect params errors --- src/error.rs | 11 ++++++----- src/lib.rs | 26 ++++++++++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/error.rs b/src/error.rs index d3207ab6..dcab5feb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -193,8 +193,8 @@ impl error::Error for DbError { /// Reasons a new Postgres connection could fail. #[derive(Debug)] pub enum ConnectError { - /// The provided URL could not be parsed. - InvalidUrl(String), + /// An error creating `ConnectParams`. + BadConnectParams(Box), /// The URL was missing a user. MissingUser, /// An error from the Postgres server itself. @@ -216,7 +216,7 @@ 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), + ConnectError::BadConnectParams(ref msg) => write!(fmt, ": {}", msg), ConnectError::DbError(ref err) => write!(fmt, ": {}", err), ConnectError::SslError(ref err) => write!(fmt, ": {}", err), ConnectError::IoError(ref err) => write!(fmt, ": {}", err), @@ -228,8 +228,8 @@ impl fmt::Display for ConnectError { impl error::Error for ConnectError { fn description(&self) -> &str { match *self { - ConnectError::InvalidUrl(_) => "Invalid URL", - ConnectError::MissingUser => "User missing in URL", + ConnectError::BadConnectParams(_) => "Error creating `ConnectParams`", + ConnectError::MissingUser => "User missing in `ConnectParams`", ConnectError::DbError(_) => "Error reported by Postgres", ConnectError::MissingPassword => "The server requested a password but none was provided", ConnectError::UnsupportedAuthentication => { @@ -243,6 +243,7 @@ impl error::Error for ConnectError { fn cause(&self) -> Option<&error::Error> { match *self { + ConnectError::BadConnectParams(ref err) => Some(&**err), ConnectError::DbError(ref err) => Some(err), ConnectError::SslError(ref err) => Some(&**err), ConnectError::IoError(ref err) => Some(err), diff --git a/src/lib.rs b/src/lib.rs index ad2bcd35..7cf9490c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,10 +61,12 @@ use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::cell::{Cell, RefCell}; use std::collections::{VecDeque, HashMap}; +use std::error::Error as StdError; use std::fmt; use std::iter::IntoIterator; use std::io as std_io; use std::io::prelude::*; +use std::marker::Sync as StdSync; use std::mem; use std::result; #[cfg(feature = "unix_socket")] @@ -145,26 +147,26 @@ pub struct ConnectParams { /// A trait implemented by types that can be converted into a `ConnectParams`. pub trait IntoConnectParams { /// Converts the value of `self` into a `ConnectParams`. - fn into_connect_params(self) -> result::Result; + fn into_connect_params(self) -> result::Result>; } impl IntoConnectParams for ConnectParams { - fn into_connect_params(self) -> result::Result { + fn into_connect_params(self) -> result::Result> { Ok(self) } } impl<'a> IntoConnectParams for &'a str { - fn into_connect_params(self) -> result::Result { + fn into_connect_params(self) -> result::Result> { match Url::parse(self) { Ok(url) => url.into_connect_params(), - Err(err) => return Err(ConnectError::InvalidUrl(err)), + Err(err) => return Err(err.into()), } } } impl IntoConnectParams for Url { - fn into_connect_params(self) -> result::Result { + fn into_connect_params(self) -> result::Result> { let Url { host, port, @@ -174,16 +176,16 @@ impl IntoConnectParams for Url { } = self; #[cfg(feature = "unix_socket")] - fn make_unix(maybe_path: String) -> result::Result { + fn make_unix(maybe_path: String) + -> result::Result> { Ok(ConnectTarget::Unix(PathBuf::from(maybe_path))) } #[cfg(not(feature = "unix_socket"))] - fn make_unix(_: String) -> result::Result { - Err(ConnectError::InvalidUrl("unix socket support requires the `unix_socket` feature" - .to_string())) + fn make_unix(_: String) -> result::Result> { + Err("unix socket support requires the `unix_socket` feature".into()) } - let maybe_path = try!(url::decode_component(&host).map_err(ConnectError::InvalidUrl)); + let maybe_path = try!(url::decode_component(&host)); let target = if maybe_path.starts_with("/") { try!(make_unix(maybe_path)) } else { @@ -330,7 +332,7 @@ pub struct CancelData { pub fn cancel_query(params: T, ssl: &SslMode, data: CancelData) -> result::Result<(), ConnectError> where T: IntoConnectParams { - let params = try!(params.into_connect_params()); + let params = try!(params.into_connect_params().map_err(ConnectError::BadConnectParams)); let mut socket = try!(priv_io::initialize_stream(¶ms, ssl)); try!(socket.write_message(&CancelRequest { @@ -459,7 +461,7 @@ impl Drop for InnerConnection { impl InnerConnection { fn connect(params: T, ssl: &SslMode) -> result::Result where T: IntoConnectParams { - let params = try!(params.into_connect_params()); + let params = try!(params.into_connect_params().map_err(ConnectError::BadConnectParams)); let stream = try!(priv_io::initialize_stream(¶ms, ssl)); let ConnectParams { user, database, mut options, .. } = params;