Skip to content

Commit 978228d

Browse files
committed
Implement Error for errors
1 parent b458e85 commit 978228d

1 file changed

Lines changed: 91 additions & 2 deletions

File tree

src/error.rs

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Postgres errors
22
33
use std::collections::HashMap;
4+
use std::error;
45
use std::io;
56
use std::fmt;
67
use std::result;
78

8-
use openssl::ssl::error;
9+
use openssl::ssl::error as sslerror;
910
use phf;
1011

1112
use Result;
@@ -375,13 +376,49 @@ pub enum ConnectError {
375376
/// The Postgres server does not support SSL encryption
376377
NoSslSupport,
377378
/// There was an error initializing the SSL session
378-
SslError(error::SslError),
379+
SslError(sslerror::SslError),
379380
/// There was an error communicating with the server
380381
PgConnectStreamError(io::IoError),
381382
/// The server sent an unexpected response
382383
PgConnectBadResponse,
383384
}
384385

386+
impl error::Error for ConnectError {
387+
fn description(&self) -> &str {
388+
match *self {
389+
InvalidUrl(_) => "Invalid URL",
390+
MissingUser => "User missing in URL",
391+
SocketError(_) => "Unable to open connection to server",
392+
PgConnectDbError(_) => "An error from the Postgres server itself",
393+
MissingPassword => "The server requested a password but none was provided",
394+
UnsupportedAuthentication => {
395+
"The server requested an unsupported authentication method"
396+
}
397+
NoSslSupport => "The server does not support SSL",
398+
SslError(_) => "Error initiating SSL session",
399+
PgConnectStreamError(_) => "Error communicating with server",
400+
PgConnectBadResponse => "The server returned an unexpected response",
401+
}
402+
}
403+
404+
fn detail(&self) -> Option<String> {
405+
match *self {
406+
InvalidUrl(ref msg) => Some(msg.clone()),
407+
_ => None,
408+
}
409+
}
410+
411+
fn cause(&self) -> Option<&error::Error> {
412+
match *self {
413+
SocketError(ref err) => Some(err as &error::Error),
414+
PgConnectDbError(ref err) => Some(err as &error::Error),
415+
SslError(ref err) => Some(err as &error::Error),
416+
PgConnectStreamError(ref err) => Some(err as &error::Error),
417+
_ => None
418+
}
419+
}
420+
}
421+
385422
impl fmt::Show for ConnectError {
386423
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
387424
match *self {
@@ -531,6 +568,16 @@ impl fmt::Show for DbError {
531568
}
532569
}
533570

571+
impl error::Error for DbError {
572+
fn description(&self) -> &str {
573+
&*self.message
574+
}
575+
576+
fn detail(&self) -> Option<String> {
577+
self.detail.clone()
578+
}
579+
}
580+
534581
/// An error encountered when communicating with the Postgres server
535582
#[deriving(Clone, PartialEq, Eq)]
536583
pub enum Error {
@@ -591,3 +638,45 @@ impl fmt::Show for Error {
591638
}
592639
}
593640
}
641+
642+
impl error::Error for Error {
643+
fn description(&self) -> &str {
644+
match *self {
645+
PgDbError(_) => "An error reported by the Postgres server",
646+
PgStreamError(_) => "An error communicating with the Postgres server",
647+
PgStreamDesynchronized => {
648+
"Communication with the server has desynchronized due to an earlier IO error"
649+
}
650+
PgWrongConnection => {
651+
"A statement was executed with a connection with which it was not prepared"
652+
}
653+
PgWrongParamCount { .. } => "Wrong number of parameters",
654+
PgWrongType(_) => "Unexpected type",
655+
PgInvalidColumn => "Invalid column",
656+
PgWasNull => "The value was NULL",
657+
PgWrongTransaction => {
658+
"An attempt was made to use an object other than the active transaction"
659+
}
660+
PgBadResponse => "The server returned an unexpected response",
661+
PgBadData => "The server provided data that the client could not parse",
662+
}
663+
}
664+
665+
fn detail(&self) -> Option<String> {
666+
match *self {
667+
PgWrongParamCount { expected, actual } => {
668+
Some(format!("expected: {}, actual: {}", expected, actual))
669+
}
670+
PgWrongType(ref ty) => Some(format!("saw type {}", ty)),
671+
_ => None
672+
}
673+
}
674+
675+
fn cause(&self) -> Option<&error::Error> {
676+
match *self {
677+
PgDbError(ref err) => Some(err as &error::Error),
678+
PgStreamError(ref err) => Some(err as &error::Error),
679+
_ => None
680+
}
681+
}
682+
}

0 commit comments

Comments
 (0)