Skip to content

Commit a4fe4e2

Browse files
committed
Flatten and rename error stuff
1 parent 056f90a commit a4fe4e2

File tree

4 files changed

+80
-82
lines changed

4 files changed

+80
-82
lines changed

src/error.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ macro_rules! make_errors(
1616
/// SQLSTATE error codes
1717
#[deriving(PartialEq, Eq, Clone, Show)]
1818
#[allow(missing_docs)]
19-
pub enum PostgresSqlState {
19+
pub enum SqlState {
2020
$($error,)+
21-
UnknownSqlState(String)
21+
Unknown(String)
2222
}
2323

24-
static STATE_MAP: phf::Map<&'static str, PostgresSqlState> = phf_map!(
25-
$($code => $error),+
24+
static STATE_MAP: phf::Map<&'static str, SqlState> = phf_map!(
25+
$($code => SqlState::$error),+
2626
);
2727

28-
impl PostgresSqlState {
28+
impl SqlState {
2929
#[doc(hidden)]
30-
pub fn from_code(s: &str) -> PostgresSqlState {
30+
pub fn from_code(s: &str) -> SqlState {
3131
match STATE_MAP.find_equiv(s) {
3232
Some(state) => state.clone(),
33-
None => UnknownSqlState(s.into_string())
33+
None => SqlState::Unknown(s.into_string())
3434
}
3535
}
3636
}
@@ -358,15 +358,15 @@ make_errors!(
358358

359359
/// Reasons a new Postgres connection could fail
360360
#[deriving(Clone, PartialEq, Eq)]
361-
pub enum PostgresConnectError {
361+
pub enum ConnectError {
362362
/// The provided URL could not be parsed
363363
InvalidUrl(String),
364364
/// The URL was missing a user
365365
MissingUser,
366366
/// There was an error opening a socket to the server
367367
SocketError(io::IoError),
368368
/// An error from the Postgres server itself
369-
PgConnectDbError(PostgresDbError),
369+
PgConnectDbError(DbError),
370370
/// A password was required but not provided in the URL
371371
MissingPassword,
372372
/// The Postgres server requested an authentication method not supported
@@ -382,7 +382,7 @@ pub enum PostgresConnectError {
382382
PgConnectBadResponse,
383383
}
384384

385-
impl fmt::Show for PostgresConnectError {
385+
impl fmt::Show for ConnectError {
386386
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
387387
match *self {
388388
InvalidUrl(ref err) => write!(fmt, "Invalid URL: {}", err),
@@ -408,11 +408,11 @@ impl fmt::Show for PostgresConnectError {
408408

409409
/// Represents the position of an error in a query
410410
#[deriving(Clone, PartialEq, Eq)]
411-
pub enum PostgresErrorPosition {
411+
pub enum ErrorPosition {
412412
/// A position in the original query
413-
Position(uint),
413+
Normal(uint),
414414
/// A position in an internally generated query
415-
InternalPosition {
415+
Internal {
416416
/// The byte position
417417
pub position: uint,
418418
/// A query generated by the Postgres server
@@ -422,13 +422,13 @@ pub enum PostgresErrorPosition {
422422

423423
/// Encapsulates a Postgres error or notice.
424424
#[deriving(Clone, PartialEq, Eq)]
425-
pub struct PostgresDbError {
425+
pub struct DbError {
426426
/// The field contents are ERROR, FATAL, or PANIC (in an error message),
427427
/// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a
428428
/// localized translation of one of these.
429429
pub severity: String,
430430
/// The SQLSTATE code for the error.
431-
pub code: PostgresSqlState,
431+
pub code: SqlState,
432432
/// The primary human-readable error message. This should be accurate but
433433
/// terse (typically one line).
434434
pub message: String,
@@ -441,7 +441,7 @@ pub struct PostgresDbError {
441441
pub hint: Option<String>,
442442
/// An optional error cursor position into either the original query string
443443
/// or an internally generated query.
444-
pub position: Option<PostgresErrorPosition>,
444+
pub position: Option<ErrorPosition>,
445445
/// An indication of the context in which the error occurred. Presently
446446
/// this includes a call stack traceback of active procedural language
447447
/// functions and internally-generated queries. The trace is one entry per
@@ -476,20 +476,20 @@ pub struct PostgresDbError {
476476
pub routine: String
477477
}
478478

479-
impl PostgresDbError {
479+
impl DbError {
480480
#[doc(hidden)]
481-
pub fn new_raw(fields: Vec<(u8, String)>) -> result::Result<PostgresDbError, ()> {
481+
pub fn new_raw(fields: Vec<(u8, String)>) -> result::Result<DbError, ()> {
482482
let mut map: HashMap<_, _> = fields.into_iter().collect();
483-
Ok(PostgresDbError {
483+
Ok(DbError {
484484
severity: try!(map.pop(&b'S').ok_or(())),
485-
code: PostgresSqlState::from_code(try!(map.pop(&b'C').ok_or(()))[]),
485+
code: SqlState::from_code(try!(map.pop(&b'C').ok_or(()))[]),
486486
message: try!(map.pop(&b'M').ok_or(())),
487487
detail: map.pop(&b'D'),
488488
hint: map.pop(&b'H'),
489489
position: match map.pop(&b'P') {
490-
Some(pos) => Some(Position(try!(from_str(pos[]).ok_or(())))),
490+
Some(pos) => Some(ErrorPosition::Normal(try!(from_str(pos[]).ok_or(())))),
491491
None => match map.pop(&b'p') {
492-
Some(pos) => Some(InternalPosition {
492+
Some(pos) => Some(ErrorPosition::Internal {
493493
position: try!(from_str(pos[]).ok_or(())),
494494
query: try!(map.pop(&b'q').ok_or(()))
495495
}),
@@ -509,33 +509,33 @@ impl PostgresDbError {
509509
}
510510

511511
#[doc(hidden)]
512-
pub fn new_connect<T>(fields: Vec<(u8, String)>) -> result::Result<T, PostgresConnectError> {
513-
match PostgresDbError::new_raw(fields) {
512+
pub fn new_connect<T>(fields: Vec<(u8, String)>) -> result::Result<T, ConnectError> {
513+
match DbError::new_raw(fields) {
514514
Ok(err) => Err(PgConnectDbError(err)),
515515
Err(()) => Err(PgConnectBadResponse),
516516
}
517517
}
518518

519519
#[doc(hidden)]
520520
pub fn new<T>(fields: Vec<(u8, String)>) -> Result<T> {
521-
match PostgresDbError::new_raw(fields) {
521+
match DbError::new_raw(fields) {
522522
Ok(err) => Err(PgDbError(err)),
523523
Err(()) => Err(PgBadData),
524524
}
525525
}
526526
}
527527

528-
impl fmt::Show for PostgresDbError {
528+
impl fmt::Show for DbError {
529529
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
530530
write!(fmt, "{}: {}", self.severity, self.message)
531531
}
532532
}
533533

534534
/// An error encountered when communicating with the Postgres server
535535
#[deriving(Clone, PartialEq, Eq)]
536-
pub enum PostgresError {
536+
pub enum Error {
537537
/// An error reported by the Postgres server
538-
PgDbError(PostgresDbError),
538+
PgDbError(DbError),
539539
/// An error communicating with the Postgres server
540540
PgStreamError(io::IoError),
541541
/// The communication channel with the Postgres server has desynchronized
@@ -566,7 +566,7 @@ pub enum PostgresError {
566566
PgBadData,
567567
}
568568

569-
impl fmt::Show for PostgresError {
569+
impl fmt::Show for Error {
570570
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
571571
match *self {
572572
PgDbError(ref err) => err.fmt(fmt),

src/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::net::pipe;
55
use std::io::{Stream, IoResult};
66

77
use {ConnectParams, SslMode, NoSsl, PreferSsl, RequireSsl, ConnectTarget};
8-
use error::{PostgresConnectError, PgConnectStreamError, NoSslSupport, SslError, SocketError};
8+
use error::{ConnectError, PgConnectStreamError, NoSslSupport, SslError, SocketError};
99
use message;
1010
use message::{SslRequest, WriteMessage};
1111

@@ -72,7 +72,7 @@ impl Writer for InternalStream {
7272
}
7373

7474
fn open_socket(params: &ConnectParams)
75-
-> Result<InternalStream, PostgresConnectError> {
75+
-> Result<InternalStream, ConnectError> {
7676
let port = params.port.unwrap_or(DEFAULT_PORT);
7777
let socket = match params.target {
7878
ConnectTarget::Tcp(ref host) =>
@@ -87,7 +87,7 @@ fn open_socket(params: &ConnectParams)
8787
}
8888

8989
pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode)
90-
-> Result<MaybeSslStream<InternalStream>, PostgresConnectError> {
90+
-> Result<MaybeSslStream<InternalStream>, ConnectError> {
9191
let mut socket = try!(open_socket(params));
9292

9393
let (ssl_required, ctx) = match *ssl {

0 commit comments

Comments
 (0)