diff --git a/src/error.rs b/src/error.rs index bbaa0ec8..c938927a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,12 +24,13 @@ macro_rules! make_errors( $($code => $error),+ ); - impl FromStr for PostgresSqlState { - fn from_str(s: &str) -> Option { - Some(match STATE_MAP.find(&s) { + impl PostgresSqlState { + #[doc(hidden)] + pub fn from_code(s: &str) -> PostgresSqlState { + match STATE_MAP.find(&s) { Some(state) => state.clone(), None => UnknownSqlState(s.to_owned()) - }) + } } } ) @@ -362,9 +363,9 @@ pub enum PostgresConnectError { /// The URL was missing a user MissingUser, /// DNS lookup failed - DnsError, + DnsError(IoError), /// There was an error opening a socket to the server - SocketError, + SocketError(IoError), /// An error from the Postgres server itself PgConnectDbError(PostgresDbError), /// A password was required but not provided in the URL @@ -385,9 +386,9 @@ impl fmt::Show for PostgresConnectError { match *self { InvalidUrl => fmt.buf.write_str("Invalid URL"), MissingUser => fmt.buf.write_str("User missing in URL"), - DnsError => fmt.buf.write_str("DNS lookup failed"), - SocketError => - fmt.buf.write_str("Unable to open connection to server"), + DnsError(ref err) => write!(fmt.buf, "DNS lookup failed: {}", err), + SocketError(ref err) => + write!(fmt.buf, "Unable to open connection to server: {}", err), PgConnectDbError(ref err) => err.fmt(fmt), MissingPassword => fmt.buf.write_str("The server requested a password but none \ @@ -397,8 +398,10 @@ impl fmt::Show for PostgresConnectError { authentication method"), NoSslSupport => fmt.buf.write_str("The server does not support SSL"), - SslError(ref err) => err.fmt(fmt), - PgConnectStreamError(ref err) => err.fmt(fmt), + SslError(ref err) => + write!(fmt.buf, "Error initiating SSL session: {}", err), + PgConnectStreamError(ref err) => + write!(fmt.buf, "Error communicating with server: {}", err), } } } @@ -477,7 +480,7 @@ impl PostgresDbError { let mut map: HashMap = fields.move_iter().collect(); PostgresDbError { severity: map.pop(&('S' as u8)).unwrap(), - code: FromStr::from_str(map.pop(&('C' as u8)).unwrap()).unwrap(), + code: PostgresSqlState::from_code(map.pop(&('C' as u8)).unwrap()), message: map.pop(&('M' as u8)).unwrap(), detail: map.pop(&('D' as u8)), hint: map.pop(&('H' as u8)), diff --git a/src/lib.rs b/src/lib.rs index 5367fad9..536bf1a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -268,6 +268,8 @@ pub struct PostgresCancelData { /// `PostgresConnection::cancel_data`. The object can cancel any query made on /// that connection. /// +/// Only the host and port of the URL are used. +/// /// # Example /// /// ```rust,no_run @@ -311,17 +313,18 @@ fn open_socket(host: &str, port: Port) -> Result { let addrs = match net::get_host_addresses(host) { Ok(addrs) => addrs, - Err(_) => return Err(DnsError) + Err(err) => return Err(DnsError(err)) }; + let mut err = None; for &addr in addrs.iter() { match TcpStream::connect(SocketAddr { ip: addr, port: port }) { Ok(socket) => return Ok(socket), - Err(_) => {} + Err(e) => err = Some(e) } } - Err(SocketError) + Err(SocketError(err.unwrap())) } fn initialize_stream(host: &str, port: Port, ssl: &SslMode) @@ -1356,7 +1359,7 @@ impl<'stmt> Container for PostgresRow<'stmt> { } } -impl<'stmt, I: RowIndex, T: FromSql> Index for PostgresRow<'stmt> { +impl<'stmt, I: RowIndex+Clone, T: FromSql> Index for PostgresRow<'stmt> { /// Retreives the contents of a field of the row. /// /// A field can be accessed by the name or index of its column, though @@ -1387,7 +1390,7 @@ impl<'stmt, I: RowIndex, T: FromSql> Index for PostgresRow<'stmt> { } /// A trait implemented by types that can index into columns of a row. -pub trait RowIndex: Clone { +pub trait RowIndex { /// Returns the index of the appropriate column, or `None` if no such /// column exists. fn idx(&self, stmt: &PostgresStatement) -> Option; diff --git a/src/test.rs b/src/test.rs index aaf350b3..02fdeeae 100644 --- a/src/test.rs +++ b/src/test.rs @@ -897,7 +897,7 @@ fn test_plaintext_pass_no_pass() { let ret = PostgresConnection::connect("postgres://pass_user@localhost/postgres", &NoSsl); match ret { Err(MissingPassword) => (), - Err(err) => fail!("Unexpected error {}", err.to_str()), + Err(err) => fail!("Unexpected error {}", err), _ => fail!("Expected error") } } @@ -907,7 +907,7 @@ fn test_plaintext_pass_wrong_pass() { let ret = PostgresConnection::connect("postgres://pass_user:asdf@localhost/postgres", &NoSsl); match ret { Err(PgConnectDbError(PostgresDbError { code: InvalidPassword, .. })) => (), - Err(err) => fail!("Unexpected error {}", err.to_str()), + Err(err) => fail!("Unexpected error {}", err), _ => fail!("Expected error") } } @@ -922,7 +922,7 @@ fn test_md5_pass_no_pass() { let ret = PostgresConnection::connect("postgres://md5_user@localhost/postgres", &NoSsl); match ret { Err(MissingPassword) => (), - Err(err) => fail!("Unexpected error {}", err.to_str()), + Err(err) => fail!("Unexpected error {}", err), _ => fail!("Expected error") } } @@ -932,7 +932,7 @@ fn test_md5_pass_wrong_pass() { let ret = PostgresConnection::connect("postgres://md5_user:asdf@localhost/postgres", &NoSsl); match ret { Err(PgConnectDbError(PostgresDbError { code: InvalidPassword, .. })) => (), - Err(err) => fail!("Unexpected error {}", err.to_str()), + Err(err) => fail!("Unexpected error {}", err), _ => fail!("Expected error") } } @@ -941,8 +941,8 @@ fn test_md5_pass_wrong_pass() { fn test_dns_failure() { let ret = PostgresConnection::connect("postgres://postgres@asdfasdfasdf", &NoSsl); match ret { - Err(DnsError) => (), - Err(err) => fail!("Unexpected error {}", err.to_str()), + Err(DnsError(_)) => (), + Err(err) => fail!("Unexpected error {}", err), _ => fail!("Expected error") } } diff --git a/src/types/range.rs b/src/types/range.rs index 9bf7aa1e..208885f0 100644 --- a/src/types/range.rs +++ b/src/types/range.rs @@ -184,7 +184,7 @@ impl Clone for RangeBound { } impl fmt::Show for RangeBound { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let chars = match self.type_ { Inclusive => ['[', ']'], Exclusive => ['(', ')'], @@ -192,12 +192,10 @@ impl fmt::Show for RangeBound { match BoundSided::side(None::) { Lower => { - try!(formatter.buf.write_char(chars[0])); - self.value.fmt(formatter) + write!(fmt.buf, "{}{}", chars[0], self.value) } Upper => { - try!(self.value.fmt(formatter)); - formatter.buf.write_char(chars[1]) + write!(fmt.buf, "{}{}", self.value, chars[1]) } } } @@ -270,18 +268,18 @@ enum InnerRange { } impl fmt::Show for Range { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match self.inner { - Empty => formatter.buf.write_str("empty"), + Empty => fmt.buf.write_str("empty"), Normal(ref lower, ref upper) => { match *lower { - Some(ref bound) => try!(bound.fmt(formatter)), - None => try!(formatter.buf.write_char('(')), + Some(ref bound) => try!(bound.fmt(fmt)), + None => try!(fmt.buf.write_char('(')), } - try!(formatter.buf.write_char(',')); + try!(fmt.buf.write_char(',')); match *upper { - Some(ref bound) => bound.fmt(formatter), - None => formatter.buf.write_char(')'), + Some(ref bound) => bound.fmt(fmt), + None => fmt.buf.write_char(')'), } } }