Skip to content

Commit 99e759b

Browse files
committed
Custom Show impls for errors
1 parent b930bc3 commit 99e759b

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

src/error.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use collections::HashMap;
44
use std::from_str::FromStr;
55
use std::io::IoError;
6+
use std::fmt;
67

78
use openssl::ssl::error::SslError;
89
use phf::PhfMap;
@@ -12,7 +13,7 @@ use types::PostgresType;
1213
macro_rules! make_errors(
1314
($($code:expr => $error:ident),+) => (
1415
/// SQLSTATE error codes
15-
#[deriving(Eq, Clone, Show)]
16+
#[deriving(Eq, Clone)]
1617
#[allow(missing_doc)]
1718
pub enum PostgresSqlState {
1819
$($error,)+
@@ -355,7 +356,6 @@ make_errors!(
355356
)
356357

357358
/// Reasons a new Postgres connection could fail
358-
#[deriving(Show)]
359359
pub enum PostgresConnectError {
360360
/// The provided URL could not be parsed
361361
InvalidUrl,
@@ -380,8 +380,30 @@ pub enum PostgresConnectError {
380380
PgConnectStreamError(IoError),
381381
}
382382

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+
383406
/// Represents the position of an error in a query
384-
#[deriving(Show)]
385407
pub enum PostgresErrorPosition {
386408
/// A position in the original query
387409
Position(uint),
@@ -395,7 +417,6 @@ pub enum PostgresErrorPosition {
395417
}
396418

397419
/// Encapsulates a Postgres error or notice.
398-
#[deriving(Show)]
399420
pub struct PostgresDbError {
400421
/// The field contents are ERROR, FATAL, or PANIC (in an error message),
401422
/// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a
@@ -483,8 +504,13 @@ impl PostgresDbError {
483504
}
484505
}
485506

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+
486513
/// An error encountered when communicating with the Postgres server
487-
#[deriving(Show)]
488514
pub enum PostgresError {
489515
/// An error reported by the Postgres server
490516
PgDbError(PostgresDbError),
@@ -510,3 +536,24 @@ pub enum PostgresError {
510536
/// A value was NULL but converted to a non-nullable Rust type
511537
PgWasNull,
512538
}
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

Comments
 (0)