pub use ugh_privacy::DbError;
use byteorder;
use openssl::ssl::error::SslError;
use phf;
use std::error;
use std::convert::From;
use std::fmt;
use std::io;
use Result;
use types::Type;
include!(concat!(env!("OUT_DIR"), "/sqlstate.rs"));
#[derive(Debug)]
pub enum ConnectError {
InvalidUrl(String),
MissingUser,
DbError(DbError),
MissingPassword,
UnsupportedAuthentication,
NoSslSupport,
SslError(SslError),
IoError(io::Error),
BadResponse,
}
impl fmt::Display for ConnectError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(fmt.write_str(error::Error::description(self)));
match *self {
ConnectError::InvalidUrl(ref msg) => write!(fmt, ": {}", msg),
_ => Ok(())
}
}
}
impl error::Error for ConnectError {
fn description(&self) -> &str {
match *self {
ConnectError::InvalidUrl(_) => "Invalid URL",
ConnectError::MissingUser => "User missing in URL",
ConnectError::DbError(_) => "An error from the Postgres server itself",
ConnectError::MissingPassword => "The server requested a password but none was provided",
ConnectError::UnsupportedAuthentication => {
"The server requested an unsupported authentication method"
}
ConnectError::NoSslSupport => "The server does not support SSL",
ConnectError::SslError(_) => "Error initiating SSL session",
ConnectError::IoError(_) => "Error communicating with server",
ConnectError::BadResponse => "The server returned an unexpected response",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
ConnectError::DbError(ref err) => Some(err),
ConnectError::SslError(ref err) => Some(err),
ConnectError::IoError(ref err) => Some(err),
_ => None
}
}
}
impl From<io::Error> for ConnectError {
fn from(err: io::Error) -> ConnectError {
ConnectError::IoError(err)
}
}
impl From<DbError> for ConnectError {
fn from(err: DbError) -> ConnectError {
ConnectError::DbError(err)
}
}
impl From<SslError> for ConnectError {
fn from(err: SslError) -> ConnectError {
ConnectError::SslError(err)
}
}
impl From<byteorder::Error> for ConnectError {
fn from(err: byteorder::Error) -> ConnectError {
ConnectError::IoError(From::from(err))
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ErrorPosition {
Normal(u32),
Internal {
position: u32,
query: String
}
}
#[derive(Debug)]
pub enum Error {
DbError(DbError),
IoError(io::Error),
StreamDesynchronized,
WrongType(Type),
InvalidColumn,
WasNull,
BadResponse,
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(fmt.write_str(error::Error::description(self)));
match *self {
Error::WrongType(ref ty) => write!(fmt, ": saw type {:?}", ty),
_ => Ok(()),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::DbError(_) => "An error reported by the Postgres server",
Error::IoError(_) => "An error communicating with the Postgres server",
Error::StreamDesynchronized => {
"Communication with the server has desynchronized due to an earlier IO error"
}
Error::WrongType(_) => "Unexpected type",
Error::InvalidColumn => "Invalid column",
Error::WasNull => "The value was NULL",
Error::BadResponse => "The server returned an unexpected response",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::DbError(ref err) => Some(err),
Error::IoError(ref err) => Some(err),
_ => None
}
}
}
impl From<DbError> for Error {
fn from(err: DbError) -> Error {
Error::DbError(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::IoError(err)
}
}
impl From<byteorder::Error> for Error {
fn from(err: byteorder::Error) -> Error {
Error::IoError(From::from(err))
}
}