Skip to content

Commit 018c896

Browse files
committed
Update for r2d2 changes
1 parent 41c386c commit 018c896

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/lib.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
extern crate r2d2;
22
extern crate postgres;
33

4+
use std::fmt;
45
use postgres::{PostgresConnection, PostgresConnectParams, IntoConnectParams, SslMode};
5-
use postgres::error::PostgresConnectError;
6+
use postgres::error::{PostgresConnectError, PostgresError};
7+
8+
pub enum Error {
9+
ConnectError(PostgresConnectError),
10+
OtherError(PostgresError),
11+
}
12+
13+
impl fmt::Show for Error {
14+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
15+
match *self {
16+
ConnectError(ref e) => write!(fmt, "{}", e),
17+
OtherError(ref e) => write!(fmt, "{}", e),
18+
}
19+
}
20+
}
621

722
pub struct PostgresPoolManager {
823
params: Result<PostgresConnectParams, PostgresConnectError>,
@@ -18,16 +33,18 @@ impl PostgresPoolManager {
1833
}
1934
}
2035

21-
impl r2d2::PoolManager<PostgresConnection, PostgresConnectError> for PostgresPoolManager {
22-
fn connect(&self) -> Result<PostgresConnection, PostgresConnectError> {
36+
impl r2d2::PoolManager<PostgresConnection, Error> for PostgresPoolManager {
37+
fn connect(&self) -> Result<PostgresConnection, Error> {
2338
match self.params {
24-
Ok(ref p) => PostgresConnection::connect(p.clone(), &self.ssl_mode),
25-
Err(ref e) => Err(e.clone())
39+
Ok(ref p) => {
40+
PostgresConnection::connect(p.clone(), &self.ssl_mode).map_err(ConnectError)
41+
}
42+
Err(ref e) => Err(ConnectError(e.clone()))
2643
}
2744
}
2845

29-
fn is_valid(&self, conn: &mut PostgresConnection) -> bool {
30-
conn.batch_execute("SELECT 1").is_ok()
46+
fn is_valid(&self, conn: &mut PostgresConnection) -> Result<(), Error> {
47+
conn.batch_execute("SELECT 1").map_err(OtherError)
3148
}
3249

3350
fn has_broken(&self, conn: &mut PostgresConnection) -> bool {

tests/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use std::comm;
88

99
use postgres::NoSsl;
1010
use postgres::error::{SocketError, InvalidUrl};
11-
use r2d2_postgres::PostgresPoolManager;
11+
use r2d2_postgres::{PostgresPoolManager, ConnectError};
1212

1313
#[test]
1414
fn test_bad_url_deferred() {
1515
let manager = PostgresPoolManager::new("not a url", NoSsl);
1616
let config = Default::default();
1717
let handler = r2d2::NoopErrorHandler;
1818
match r2d2::Pool::new(config, manager, handler) {
19-
Err(r2d2::ConnectionError(InvalidUrl(_))) => {}
19+
Err(r2d2::ConnectionError(ConnectError(InvalidUrl(_)))) => {}
2020
Err(err) => fail!("Unexpected error {}", err),
2121
_ => fail!("Unexpected success"),
2222
}
@@ -28,7 +28,7 @@ fn test_bad_host_error() {
2828
let config = Default::default();
2929
let handler = r2d2::NoopErrorHandler;
3030
match r2d2::Pool::new(config, manager, handler) {
31-
Err(r2d2::ConnectionError(SocketError(_))) => {}
31+
Err(r2d2::ConnectionError(ConnectError(SocketError(_)))) => {}
3232
Err(err) => fail!("Unexpected error {}", err),
3333
_ => fail!("Unexpected success")
3434
}

0 commit comments

Comments
 (0)