3
3
use collections:: HashMap ;
4
4
use std:: from_str:: FromStr ;
5
5
use std:: io:: IoError ;
6
+ use std:: fmt;
6
7
7
8
use openssl:: ssl:: error:: SslError ;
8
9
use phf:: PhfMap ;
@@ -12,7 +13,7 @@ use types::PostgresType;
12
13
macro_rules! make_errors(
13
14
( $( $code: expr => $error: ident) ,+) => (
14
15
/// SQLSTATE error codes
15
- #[ deriving( Eq , Clone , Show ) ]
16
+ #[ deriving( Eq , Clone ) ]
16
17
#[ allow( missing_doc) ]
17
18
pub enum PostgresSqlState {
18
19
$( $error, ) +
@@ -355,7 +356,6 @@ make_errors!(
355
356
)
356
357
357
358
/// Reasons a new Postgres connection could fail
358
- #[ deriving( Show ) ]
359
359
pub enum PostgresConnectError {
360
360
/// The provided URL could not be parsed
361
361
InvalidUrl ,
@@ -380,8 +380,30 @@ pub enum PostgresConnectError {
380
380
PgConnectStreamError ( IoError ) ,
381
381
}
382
382
383
+ impl fmt:: Show for PostgresConnectError {
384
+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
385
+ match * self {
386
+ InvalidUrl => fmt. buf . write_str ( "Invalid URL" ) ,
387
+ MissingUser => fmt. buf . write_str ( "User missing in URL" ) ,
388
+ DnsError => fmt. buf . write_str ( "DNS lookup failed" ) ,
389
+ SocketError =>
390
+ fmt. buf . write_str ( "Unable to open connection to server" ) ,
391
+ PgConnectDbError ( ref err) => err. fmt ( fmt) ,
392
+ MissingPassword =>
393
+ fmt. buf . write_str ( "The server requested a password but none \
394
+ was provided") ,
395
+ UnsupportedAuthentication =>
396
+ fmt. buf . write_str ( "The server requested an unsupporeted \
397
+ authentication method") ,
398
+ NoSslSupport =>
399
+ fmt. buf . write_str ( "The server does not support SSL" ) ,
400
+ SslError ( ref err) => err. fmt ( fmt) ,
401
+ PgConnectStreamError ( ref err) => err. fmt ( fmt) ,
402
+ }
403
+ }
404
+ }
405
+
383
406
/// Represents the position of an error in a query
384
- #[ deriving( Show ) ]
385
407
pub enum PostgresErrorPosition {
386
408
/// A position in the original query
387
409
Position ( uint ) ,
@@ -395,7 +417,6 @@ pub enum PostgresErrorPosition {
395
417
}
396
418
397
419
/// Encapsulates a Postgres error or notice.
398
- #[ deriving( Show ) ]
399
420
pub struct PostgresDbError {
400
421
/// The field contents are ERROR, FATAL, or PANIC (in an error message),
401
422
/// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a
@@ -483,8 +504,13 @@ impl PostgresDbError {
483
504
}
484
505
}
485
506
507
+ impl fmt:: Show for PostgresDbError {
508
+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
509
+ write ! ( fmt. buf, "{}: {}" , self . severity, self . message)
510
+ }
511
+ }
512
+
486
513
/// An error encountered when communicating with the Postgres server
487
- #[ deriving( Show ) ]
488
514
pub enum PostgresError {
489
515
/// An error reported by the Postgres server
490
516
PgDbError ( PostgresDbError ) ,
@@ -510,3 +536,24 @@ pub enum PostgresError {
510
536
/// A value was NULL but converted to a non-nullable Rust type
511
537
PgWasNull ,
512
538
}
539
+
540
+ impl fmt:: Show for PostgresError {
541
+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
542
+ match * self {
543
+ PgDbError ( ref err) => err. fmt ( fmt) ,
544
+ PgStreamError ( ref err) => err. fmt ( fmt) ,
545
+ PgStreamDesynchronized => fmt. buf . write_str (
546
+ "Communication with the server has desynchronized due to an \
547
+ earlier IO error") ,
548
+ PgWrongConnection => fmt. buf . write_str (
549
+ "A statement was executed with a connection it was not \
550
+ prepared with") ,
551
+ PgWrongParamCount { expected, actual } =>
552
+ write ! ( fmt. buf, "Expected {} parameters but got {}" , expected,
553
+ actual) ,
554
+ PgWrongType ( ref ty) => write ! ( fmt. buf, "Unexpected type {}" , ty) ,
555
+ PgInvalidColumn => fmt. buf . write_str ( "Invalid column" ) ,
556
+ PgWasNull => fmt. buf . write_str ( "The value was NULL" )
557
+ }
558
+ }
559
+ }
0 commit comments