11extern crate r2d2;
22extern crate postgres;
33
4+ use std:: fmt;
45use 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
722pub 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 {
0 commit comments