Flatten and rename error stuff

This commit is contained in:
Steven Fackler 2014-11-03 22:24:11 -08:00
parent 056f90a3a2
commit a4fe4e2056
4 changed files with 80 additions and 82 deletions

View File

@ -16,21 +16,21 @@ macro_rules! make_errors(
/// SQLSTATE error codes /// SQLSTATE error codes
#[deriving(PartialEq, Eq, Clone, Show)] #[deriving(PartialEq, Eq, Clone, Show)]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum PostgresSqlState { pub enum SqlState {
$($error,)+ $($error,)+
UnknownSqlState(String) Unknown(String)
} }
static STATE_MAP: phf::Map<&'static str, PostgresSqlState> = phf_map!( static STATE_MAP: phf::Map<&'static str, SqlState> = phf_map!(
$($code => $error),+ $($code => SqlState::$error),+
); );
impl PostgresSqlState { impl SqlState {
#[doc(hidden)] #[doc(hidden)]
pub fn from_code(s: &str) -> PostgresSqlState { pub fn from_code(s: &str) -> SqlState {
match STATE_MAP.find_equiv(s) { match STATE_MAP.find_equiv(s) {
Some(state) => state.clone(), Some(state) => state.clone(),
None => UnknownSqlState(s.into_string()) None => SqlState::Unknown(s.into_string())
} }
} }
} }
@ -358,7 +358,7 @@ make_errors!(
/// Reasons a new Postgres connection could fail /// Reasons a new Postgres connection could fail
#[deriving(Clone, PartialEq, Eq)] #[deriving(Clone, PartialEq, Eq)]
pub enum PostgresConnectError { pub enum ConnectError {
/// The provided URL could not be parsed /// The provided URL could not be parsed
InvalidUrl(String), InvalidUrl(String),
/// The URL was missing a user /// The URL was missing a user
@ -366,7 +366,7 @@ pub enum PostgresConnectError {
/// There was an error opening a socket to the server /// There was an error opening a socket to the server
SocketError(io::IoError), SocketError(io::IoError),
/// An error from the Postgres server itself /// An error from the Postgres server itself
PgConnectDbError(PostgresDbError), PgConnectDbError(DbError),
/// A password was required but not provided in the URL /// A password was required but not provided in the URL
MissingPassword, MissingPassword,
/// The Postgres server requested an authentication method not supported /// The Postgres server requested an authentication method not supported
@ -382,7 +382,7 @@ pub enum PostgresConnectError {
PgConnectBadResponse, PgConnectBadResponse,
} }
impl fmt::Show for PostgresConnectError { impl fmt::Show for ConnectError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
InvalidUrl(ref err) => write!(fmt, "Invalid URL: {}", err), InvalidUrl(ref err) => write!(fmt, "Invalid URL: {}", err),
@ -408,11 +408,11 @@ impl fmt::Show for PostgresConnectError {
/// Represents the position of an error in a query /// Represents the position of an error in a query
#[deriving(Clone, PartialEq, Eq)] #[deriving(Clone, PartialEq, Eq)]
pub enum PostgresErrorPosition { pub enum ErrorPosition {
/// A position in the original query /// A position in the original query
Position(uint), Normal(uint),
/// A position in an internally generated query /// A position in an internally generated query
InternalPosition { Internal {
/// The byte position /// The byte position
pub position: uint, pub position: uint,
/// A query generated by the Postgres server /// A query generated by the Postgres server
@ -422,13 +422,13 @@ pub enum PostgresErrorPosition {
/// Encapsulates a Postgres error or notice. /// Encapsulates a Postgres error or notice.
#[deriving(Clone, PartialEq, Eq)] #[deriving(Clone, PartialEq, Eq)]
pub struct PostgresDbError { pub struct DbError {
/// The field contents are ERROR, FATAL, or PANIC (in an error message), /// The field contents are ERROR, FATAL, or PANIC (in an error message),
/// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a /// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a
/// localized translation of one of these. /// localized translation of one of these.
pub severity: String, pub severity: String,
/// The SQLSTATE code for the error. /// The SQLSTATE code for the error.
pub code: PostgresSqlState, pub code: SqlState,
/// The primary human-readable error message. This should be accurate but /// The primary human-readable error message. This should be accurate but
/// terse (typically one line). /// terse (typically one line).
pub message: String, pub message: String,
@ -441,7 +441,7 @@ pub struct PostgresDbError {
pub hint: Option<String>, pub hint: Option<String>,
/// An optional error cursor position into either the original query string /// An optional error cursor position into either the original query string
/// or an internally generated query. /// or an internally generated query.
pub position: Option<PostgresErrorPosition>, pub position: Option<ErrorPosition>,
/// An indication of the context in which the error occurred. Presently /// An indication of the context in which the error occurred. Presently
/// this includes a call stack traceback of active procedural language /// this includes a call stack traceback of active procedural language
/// functions and internally-generated queries. The trace is one entry per /// functions and internally-generated queries. The trace is one entry per
@ -476,20 +476,20 @@ pub struct PostgresDbError {
pub routine: String pub routine: String
} }
impl PostgresDbError { impl DbError {
#[doc(hidden)] #[doc(hidden)]
pub fn new_raw(fields: Vec<(u8, String)>) -> result::Result<PostgresDbError, ()> { pub fn new_raw(fields: Vec<(u8, String)>) -> result::Result<DbError, ()> {
let mut map: HashMap<_, _> = fields.into_iter().collect(); let mut map: HashMap<_, _> = fields.into_iter().collect();
Ok(PostgresDbError { Ok(DbError {
severity: try!(map.pop(&b'S').ok_or(())), severity: try!(map.pop(&b'S').ok_or(())),
code: PostgresSqlState::from_code(try!(map.pop(&b'C').ok_or(()))[]), code: SqlState::from_code(try!(map.pop(&b'C').ok_or(()))[]),
message: try!(map.pop(&b'M').ok_or(())), message: try!(map.pop(&b'M').ok_or(())),
detail: map.pop(&b'D'), detail: map.pop(&b'D'),
hint: map.pop(&b'H'), hint: map.pop(&b'H'),
position: match map.pop(&b'P') { position: match map.pop(&b'P') {
Some(pos) => Some(Position(try!(from_str(pos[]).ok_or(())))), Some(pos) => Some(ErrorPosition::Normal(try!(from_str(pos[]).ok_or(())))),
None => match map.pop(&b'p') { None => match map.pop(&b'p') {
Some(pos) => Some(InternalPosition { Some(pos) => Some(ErrorPosition::Internal {
position: try!(from_str(pos[]).ok_or(())), position: try!(from_str(pos[]).ok_or(())),
query: try!(map.pop(&b'q').ok_or(())) query: try!(map.pop(&b'q').ok_or(()))
}), }),
@ -509,8 +509,8 @@ impl PostgresDbError {
} }
#[doc(hidden)] #[doc(hidden)]
pub fn new_connect<T>(fields: Vec<(u8, String)>) -> result::Result<T, PostgresConnectError> { pub fn new_connect<T>(fields: Vec<(u8, String)>) -> result::Result<T, ConnectError> {
match PostgresDbError::new_raw(fields) { match DbError::new_raw(fields) {
Ok(err) => Err(PgConnectDbError(err)), Ok(err) => Err(PgConnectDbError(err)),
Err(()) => Err(PgConnectBadResponse), Err(()) => Err(PgConnectBadResponse),
} }
@ -518,14 +518,14 @@ impl PostgresDbError {
#[doc(hidden)] #[doc(hidden)]
pub fn new<T>(fields: Vec<(u8, String)>) -> Result<T> { pub fn new<T>(fields: Vec<(u8, String)>) -> Result<T> {
match PostgresDbError::new_raw(fields) { match DbError::new_raw(fields) {
Ok(err) => Err(PgDbError(err)), Ok(err) => Err(PgDbError(err)),
Err(()) => Err(PgBadData), Err(()) => Err(PgBadData),
} }
} }
} }
impl fmt::Show for PostgresDbError { impl fmt::Show for DbError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}: {}", self.severity, self.message) write!(fmt, "{}: {}", self.severity, self.message)
} }
@ -533,9 +533,9 @@ impl fmt::Show for PostgresDbError {
/// An error encountered when communicating with the Postgres server /// An error encountered when communicating with the Postgres server
#[deriving(Clone, PartialEq, Eq)] #[deriving(Clone, PartialEq, Eq)]
pub enum PostgresError { pub enum Error {
/// An error reported by the Postgres server /// An error reported by the Postgres server
PgDbError(PostgresDbError), PgDbError(DbError),
/// An error communicating with the Postgres server /// An error communicating with the Postgres server
PgStreamError(io::IoError), PgStreamError(io::IoError),
/// The communication channel with the Postgres server has desynchronized /// The communication channel with the Postgres server has desynchronized
@ -566,7 +566,7 @@ pub enum PostgresError {
PgBadData, PgBadData,
} }
impl fmt::Show for PostgresError { impl fmt::Show for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
PgDbError(ref err) => err.fmt(fmt), PgDbError(ref err) => err.fmt(fmt),

View File

@ -5,7 +5,7 @@ use std::io::net::pipe;
use std::io::{Stream, IoResult}; use std::io::{Stream, IoResult};
use {ConnectParams, SslMode, NoSsl, PreferSsl, RequireSsl, ConnectTarget}; use {ConnectParams, SslMode, NoSsl, PreferSsl, RequireSsl, ConnectTarget};
use error::{PostgresConnectError, PgConnectStreamError, NoSslSupport, SslError, SocketError}; use error::{ConnectError, PgConnectStreamError, NoSslSupport, SslError, SocketError};
use message; use message;
use message::{SslRequest, WriteMessage}; use message::{SslRequest, WriteMessage};
@ -72,7 +72,7 @@ impl Writer for InternalStream {
} }
fn open_socket(params: &ConnectParams) fn open_socket(params: &ConnectParams)
-> Result<InternalStream, PostgresConnectError> { -> Result<InternalStream, ConnectError> {
let port = params.port.unwrap_or(DEFAULT_PORT); let port = params.port.unwrap_or(DEFAULT_PORT);
let socket = match params.target { let socket = match params.target {
ConnectTarget::Tcp(ref host) => ConnectTarget::Tcp(ref host) =>
@ -87,7 +87,7 @@ fn open_socket(params: &ConnectParams)
} }
pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode) pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode)
-> Result<MaybeSslStream<InternalStream>, PostgresConnectError> { -> Result<MaybeSslStream<InternalStream>, ConnectError> {
let mut socket = try!(open_socket(params)); let mut socket = try!(open_socket(params));
let (ssl_required, ctx) = match *ssl { let (ssl_required, ctx) = match *ssl {

View File

@ -84,9 +84,6 @@ use error::{InvalidUrl,
PgStreamDesynchronized, PgStreamDesynchronized,
PgStreamError, PgStreamError,
PgWrongParamCount, PgWrongParamCount,
PostgresConnectError,
PostgresDbError,
PostgresError,
UnsupportedAuthentication, UnsupportedAuthentication,
PgWrongConnection, PgWrongConnection,
PgWrongTransaction, PgWrongTransaction,
@ -98,6 +95,7 @@ use message::BackendMessage::*;
use message::{WriteMessage, ReadMessage}; use message::{WriteMessage, ReadMessage};
#[doc(inline)] #[doc(inline)]
pub use types::{Oid, Type, ToSql, FromSql}; pub use types::{Oid, Type, ToSql, FromSql};
pub use error::{Error, ConnectError, SqlState, DbError, ErrorPosition};
#[macro_escape] #[macro_escape]
mod macros; mod macros;
@ -106,13 +104,13 @@ mod io;
mod message; mod message;
mod url; mod url;
mod util; mod util;
pub mod error; mod error;
pub mod types; pub mod types;
const CANARY: u32 = 0xdeadbeef; const CANARY: u32 = 0xdeadbeef;
/// A typedef of the result returned by many methods. /// A typedef of the result returned by many methods.
pub type Result<T> = result::Result<T, PostgresError>; pub type Result<T> = result::Result<T, Error>;
/// Specifies the target server to connect to. /// Specifies the target server to connect to.
#[deriving(Clone)] #[deriving(Clone)]
@ -154,17 +152,17 @@ pub struct ConnectParams {
/// A trait implemented by types that can be converted into a `ConnectParams`. /// A trait implemented by types that can be converted into a `ConnectParams`.
pub trait IntoConnectParams { pub trait IntoConnectParams {
/// Converts the value of `self` into a `ConnectParams`. /// Converts the value of `self` into a `ConnectParams`.
fn into_connect_params(self) -> result::Result<ConnectParams, PostgresConnectError>; fn into_connect_params(self) -> result::Result<ConnectParams, ConnectError>;
} }
impl IntoConnectParams for ConnectParams { impl IntoConnectParams for ConnectParams {
fn into_connect_params(self) -> result::Result<ConnectParams, PostgresConnectError> { fn into_connect_params(self) -> result::Result<ConnectParams, ConnectError> {
Ok(self) Ok(self)
} }
} }
impl<'a> IntoConnectParams for &'a str { impl<'a> IntoConnectParams for &'a str {
fn into_connect_params(self) -> result::Result<ConnectParams, PostgresConnectError> { fn into_connect_params(self) -> result::Result<ConnectParams, ConnectError> {
match Url::parse(self) { match Url::parse(self) {
Ok(url) => url.into_connect_params(), Ok(url) => url.into_connect_params(),
Err(err) => return Err(InvalidUrl(err)), Err(err) => return Err(InvalidUrl(err)),
@ -173,7 +171,7 @@ impl<'a> IntoConnectParams for &'a str {
} }
impl IntoConnectParams for Url { impl IntoConnectParams for Url {
fn into_connect_params(self) -> result::Result<ConnectParams, PostgresConnectError> { fn into_connect_params(self) -> result::Result<ConnectParams, ConnectError> {
let Url { let Url {
host, host,
port, port,
@ -214,7 +212,7 @@ impl IntoConnectParams for Url {
/// Trait for types that can handle Postgres notice messages /// Trait for types that can handle Postgres notice messages
pub trait NoticeHandler { pub trait NoticeHandler {
/// Handle a Postgres notice message /// Handle a Postgres notice message
fn handle(&mut self, notice: PostgresDbError); fn handle(&mut self, notice: DbError);
} }
/// A notice handler which logs at the `info` level. /// A notice handler which logs at the `info` level.
@ -223,7 +221,7 @@ pub trait NoticeHandler {
pub struct DefaultNoticeHandler; pub struct DefaultNoticeHandler;
impl NoticeHandler for DefaultNoticeHandler { impl NoticeHandler for DefaultNoticeHandler {
fn handle(&mut self, notice: PostgresDbError) { fn handle(&mut self, notice: DbError) {
info!("{}: {}", notice.severity, notice.message); info!("{}: {}", notice.severity, notice.message);
} }
} }
@ -289,7 +287,7 @@ pub struct CancelData {
/// postgres::cancel_query(url, &NoSsl, cancel_data); /// postgres::cancel_query(url, &NoSsl, cancel_data);
/// ``` /// ```
pub fn cancel_query<T>(params: T, ssl: &SslMode, data: CancelData) pub fn cancel_query<T>(params: T, ssl: &SslMode, data: CancelData)
-> result::Result<(), PostgresConnectError> where T: IntoConnectParams { -> result::Result<(), ConnectError> where T: IntoConnectParams {
let params = try!(params.into_connect_params()); let params = try!(params.into_connect_params());
let mut socket = try!(io::initialize_stream(&params, ssl)); let mut socket = try!(io::initialize_stream(&params, ssl));
@ -326,7 +324,7 @@ impl Drop for InnerConnection {
impl InnerConnection { impl InnerConnection {
fn connect<T>(params: T, ssl: &SslMode) fn connect<T>(params: T, ssl: &SslMode)
-> result::Result<InnerConnection, PostgresConnectError> -> result::Result<InnerConnection, ConnectError>
where T: IntoConnectParams { where T: IntoConnectParams {
let params = try!(params.into_connect_params()); let params = try!(params.into_connect_params());
let stream = try!(io::initialize_stream(&params, ssl)); let stream = try!(io::initialize_stream(&params, ssl));
@ -378,7 +376,7 @@ impl InnerConnection {
conn.cancel_data.secret_key = secret_key; conn.cancel_data.secret_key = secret_key;
} }
ReadyForQuery { .. } => break, ReadyForQuery { .. } => break,
ErrorResponse { fields } => return PostgresDbError::new_connect(fields), ErrorResponse { fields } => return DbError::new_connect(fields),
_ => return Err(PgConnectBadResponse), _ => return Err(PgConnectBadResponse),
} }
} }
@ -399,7 +397,7 @@ impl InnerConnection {
loop { loop {
match try_desync!(self, self.stream.read_message()) { match try_desync!(self, self.stream.read_message()) {
NoticeResponse { fields } => { NoticeResponse { fields } => {
if let Ok(err) = PostgresDbError::new_raw(fields) { if let Ok(err) = DbError::new_raw(fields) {
self.notice_handler.handle(err); self.notice_handler.handle(err);
} }
} }
@ -418,7 +416,7 @@ impl InnerConnection {
} }
} }
fn handle_auth(&mut self, user: UserInfo) -> result::Result<(), PostgresConnectError> { fn handle_auth(&mut self, user: UserInfo) -> result::Result<(), ConnectError> {
match try_pg_conn!(self.read_message()) { match try_pg_conn!(self.read_message()) {
AuthenticationOk => return Ok(()), AuthenticationOk => return Ok(()),
AuthenticationCleartextPassword => { AuthenticationCleartextPassword => {
@ -446,7 +444,7 @@ impl InnerConnection {
| AuthenticationSCMCredential | AuthenticationSCMCredential
| AuthenticationGSS | AuthenticationGSS
| AuthenticationSSPI => return Err(UnsupportedAuthentication), | AuthenticationSSPI => return Err(UnsupportedAuthentication),
ErrorResponse { fields } => return PostgresDbError::new_connect(fields), ErrorResponse { fields } => return DbError::new_connect(fields),
_ => { _ => {
self.desynchronized = true; self.desynchronized = true;
return Err(PgConnectBadResponse); return Err(PgConnectBadResponse);
@ -455,7 +453,7 @@ impl InnerConnection {
match try_pg_conn!(self.read_message()) { match try_pg_conn!(self.read_message()) {
AuthenticationOk => Ok(()), AuthenticationOk => Ok(()),
ErrorResponse { fields } => return PostgresDbError::new_connect(fields), ErrorResponse { fields } => return DbError::new_connect(fields),
_ => { _ => {
self.desynchronized = true; self.desynchronized = true;
return Err(PgConnectBadResponse); return Err(PgConnectBadResponse);
@ -488,7 +486,7 @@ impl InnerConnection {
ParseComplete => {} ParseComplete => {}
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(self.wait_for_ready()); try!(self.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
_ => bad_response!(self), _ => bad_response!(self),
} }
@ -573,7 +571,7 @@ impl InnerConnection {
ReadyForQuery { .. } => break, ReadyForQuery { .. } => break,
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(self.wait_for_ready()); try!(self.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
_ => {} _ => {}
} }
@ -640,7 +638,7 @@ impl InnerConnection {
} }
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(self.wait_for_ready()); try!(self.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
_ => {} _ => {}
} }
@ -716,7 +714,7 @@ impl Connection {
/// let conn = try!(Connection::connect(params, &NoSsl)); /// let conn = try!(Connection::connect(params, &NoSsl));
/// # Ok(()) }; /// # Ok(()) };
/// ``` /// ```
pub fn connect<T>(params: T, ssl: &SslMode) -> result::Result<Connection, PostgresConnectError> pub fn connect<T>(params: T, ssl: &SslMode) -> result::Result<Connection, ConnectError>
where T: IntoConnectParams { where T: IntoConnectParams {
InnerConnection::connect(params, ssl).map(|conn| { InnerConnection::connect(params, ssl).map(|conn| {
Connection { conn: RefCell::new(conn) } Connection { conn: RefCell::new(conn) }
@ -808,7 +806,7 @@ impl Connection {
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # use postgres::{Connection, NoSsl}; /// # use postgres::{Connection, NoSsl};
/// # fn foo() -> Result<(), postgres::error::PostgresError> { /// # fn foo() -> Result<(), postgres::Error> {
/// # let conn = Connection::connect("", &NoSsl).unwrap(); /// # let conn = Connection::connect("", &NoSsl).unwrap();
/// let trans = try!(conn.transaction()); /// let trans = try!(conn.transaction());
/// try!(trans.execute("UPDATE foo SET bar = 10", [])); /// try!(trans.execute("UPDATE foo SET bar = 10", []));
@ -1129,7 +1127,7 @@ impl<'conn> Statement<'conn> {
BindComplete => Ok(()), BindComplete => Ok(()),
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(conn.wait_for_ready()); try!(conn.wait_for_ready());
PostgresDbError::new(fields) DbError::new(fields)
} }
_ => { _ => {
conn.desynchronized = true; conn.desynchronized = true;
@ -1196,7 +1194,7 @@ impl<'conn> Statement<'conn> {
DataRow { .. } => {} DataRow { .. } => {}
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(conn.wait_for_ready()); try!(conn.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
CommandComplete { tag } => { CommandComplete { tag } => {
num = util::parse_update_count(tag); num = util::parse_update_count(tag);
@ -1302,7 +1300,7 @@ impl<'stmt> Rows<'stmt> {
ReadyForQuery { .. } => break, ReadyForQuery { .. } => break,
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(conn.wait_for_ready()); try!(conn.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
_ => {} _ => {}
} }
@ -1326,7 +1324,7 @@ impl<'stmt> Rows<'stmt> {
DataRow { row } => self.data.push(row), DataRow { row } => self.data.push(row),
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(conn.wait_for_ready()); try!(conn.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
CopyInResponse { .. } => { CopyInResponse { .. } => {
try_pg!(conn.write_messages([ try_pg!(conn.write_messages([
@ -1555,7 +1553,7 @@ impl<'a> CopyInStatement<'a> {
BindComplete => {}, BindComplete => {},
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(conn.wait_for_ready()); try!(conn.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
_ => { _ => {
conn.desynchronized = true; conn.desynchronized = true;
@ -1633,7 +1631,7 @@ impl<'a> CopyInStatement<'a> {
CommandComplete { tag } => util::parse_update_count(tag), CommandComplete { tag } => util::parse_update_count(tag),
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(conn.wait_for_ready()); try!(conn.wait_for_ready());
return PostgresDbError::new(fields); return DbError::new(fields);
} }
_ => { _ => {
conn.desynchronized = true; conn.desynchronized = true;

View File

@ -20,24 +20,24 @@ use postgres::{NoticeHandler,
PreferSsl, PreferSsl,
NoSsl, NoSsl,
Type, Type,
ToSql}; ToSql,
use postgres::error::{PgConnectDbError, DbError};
PgDbError, use postgres::ConnectError::{PgConnectDbError,
MissingPassword};
use postgres::Error::{PgDbError,
PgWrongConnection, PgWrongConnection,
PgWrongParamCount, PgWrongParamCount,
PgWrongType, PgWrongType,
PgInvalidColumn, PgInvalidColumn,
PgWasNull, PgWasNull,
MissingPassword, PgWrongTransaction};
Position, use postgres::SqlState::{SyntaxError,
PostgresDbError, QueryCanceled,
SyntaxError, UndefinedTable,
InvalidPassword, InvalidCatalogName,
QueryCanceled, InvalidPassword,
UndefinedTable, CardinalityViolation};
InvalidCatalogName, use postgres::ErrorPosition::Normal;
PgWrongTransaction,
CardinalityViolation};
macro_rules! or_panic( macro_rules! or_panic(
($e:expr) => ( ($e:expr) => (
@ -64,7 +64,7 @@ fn test_url_terminating_slash() {
fn test_prepare_err() { fn test_prepare_err() {
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl));
match conn.prepare("invalid sql statment") { match conn.prepare("invalid sql statment") {
Err(PgDbError(PostgresDbError { code: SyntaxError, position: Some(Position(1)), .. })) => (), Err(PgDbError(DbError { code: SyntaxError, position: Some(Normal(1)), .. })) => (),
Err(e) => panic!("Unexpected result {}", e), Err(e) => panic!("Unexpected result {}", e),
_ => panic!("Unexpected result"), _ => panic!("Unexpected result"),
} }
@ -73,7 +73,7 @@ fn test_prepare_err() {
#[test] #[test]
fn test_unknown_database() { fn test_unknown_database() {
match Connection::connect("postgres://postgres@localhost/asdf", &NoSsl) { match Connection::connect("postgres://postgres@localhost/asdf", &NoSsl) {
Err(PgConnectDbError(PostgresDbError { code: InvalidCatalogName, .. })) => {} Err(PgConnectDbError(DbError { code: InvalidCatalogName, .. })) => {}
Err(resp) => panic!("Unexpected result {}", resp), Err(resp) => panic!("Unexpected result {}", resp),
_ => panic!("Unexpected result"), _ => panic!("Unexpected result"),
} }
@ -350,7 +350,7 @@ fn test_batch_execute_error() {
conn.batch_execute(query).unwrap_err(); conn.batch_execute(query).unwrap_err();
match conn.prepare("SELECT * from foo ORDER BY id") { match conn.prepare("SELECT * from foo ORDER BY id") {
Err(PgDbError(PostgresDbError { code: UndefinedTable, .. })) => {}, Err(PgDbError(DbError { code: UndefinedTable, .. })) => {},
Err(e) => panic!("unexpected error {}", e), Err(e) => panic!("unexpected error {}", e),
_ => panic!("unexpected success"), _ => panic!("unexpected success"),
} }
@ -393,7 +393,7 @@ FROM (SELECT gs.i
ORDER BY gs.i ORDER BY gs.i
LIMIT 2) ss")); LIMIT 2) ss"));
match stmt.query([]) { match stmt.query([]) {
Err(PgDbError(PostgresDbError { code: CardinalityViolation, .. })) => {} Err(PgDbError(DbError { code: CardinalityViolation, .. })) => {}
Err(err) => panic!("Unexpected error {}", err), Err(err) => panic!("Unexpected error {}", err),
Ok(_) => panic!("Expected failure"), Ok(_) => panic!("Expected failure"),
} }
@ -543,7 +543,7 @@ fn test_custom_notice_handler() {
struct Handler; struct Handler;
impl NoticeHandler for Handler { impl NoticeHandler for Handler {
fn handle(&mut self, notice: PostgresDbError) { fn handle(&mut self, notice: DbError) {
assert_eq!("note", notice.message[]); assert_eq!("note", notice.message[]);
unsafe { count += 1; } unsafe { count += 1; }
} }
@ -622,7 +622,7 @@ fn test_cancel_query() {
}); });
match conn.execute("SELECT pg_sleep(10)", []) { match conn.execute("SELECT pg_sleep(10)", []) {
Err(PgDbError(PostgresDbError { code: QueryCanceled, .. })) => {} Err(PgDbError(DbError { code: QueryCanceled, .. })) => {}
Err(res) => panic!("Unexpected result {}", res), Err(res) => panic!("Unexpected result {}", res),
_ => panic!("Unexpected result"), _ => panic!("Unexpected result"),
} }
@ -663,7 +663,7 @@ fn test_plaintext_pass_no_pass() {
fn test_plaintext_pass_wrong_pass() { fn test_plaintext_pass_wrong_pass() {
let ret = Connection::connect("postgres://pass_user:asdf@localhost/postgres", &NoSsl); let ret = Connection::connect("postgres://pass_user:asdf@localhost/postgres", &NoSsl);
match ret { match ret {
Err(PgConnectDbError(PostgresDbError { code: InvalidPassword, .. })) => (), Err(PgConnectDbError(DbError { code: InvalidPassword, .. })) => (),
Err(err) => panic!("Unexpected error {}", err), Err(err) => panic!("Unexpected error {}", err),
_ => panic!("Expected error") _ => panic!("Expected error")
} }
@ -688,7 +688,7 @@ fn test_md5_pass_no_pass() {
fn test_md5_pass_wrong_pass() { fn test_md5_pass_wrong_pass() {
let ret = Connection::connect("postgres://md5_user:asdf@localhost/postgres", &NoSsl); let ret = Connection::connect("postgres://md5_user:asdf@localhost/postgres", &NoSsl);
match ret { match ret {
Err(PgConnectDbError(PostgresDbError { code: InvalidPassword, .. })) => (), Err(PgConnectDbError(DbError { code: InvalidPassword, .. })) => (),
Err(err) => panic!("Unexpected error {}", err), Err(err) => panic!("Unexpected error {}", err),
_ => panic!("Expected error") _ => panic!("Expected error")
} }