1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use cesu8::Cesu8DecodingError;
use conn_core::ConnectionCore;
use protocol::parts::resultset::ResultSetCore;
use protocol::parts::server_error::ServerError;
use serde_db::de::{ConversionError, DeserializationError};
use serde_db::ser::SerializationError;

use std::error::{self, Error};
use std::fmt;
use std::io;
use std::result;
use std::sync;

/// Abbreviation of `Result<T, HdbError>`.
pub type HdbResult<T> = result::Result<T, HdbError>;

/// Represents all possible errors that can occur in hdbconnect.
#[derive(Debug)]
pub enum HdbError {
    // FIXME subsume into Deserialization?? -> has to be done in serde_db!
    /// Conversion of single db value to rust type failed.
    Conversion(ConversionError),

    /// Error occured in deserialization of data structures into an application-defined structure.
    Deserialization(DeserializationError),

    /// Database server responded with an error.
    DbError(ServerError),

    /// Database server has a severe issue.
    DbIssue(String),

    /// Database server responded with an error.
    MultipleDbErrors(Vec<ServerError>),

    /// Some error occured while reading CESU-8.
    Cesu8(Cesu8DecodingError),

    /// Error occured while evaluating a HdbResponse object.
    Evaluation(String),

    /// Missing or wrong implementation of HANA's wire protocol.
    Impl(String),

    /// IO error occured in communication with the database.
    Io(io::Error),

    /// Error occured in thread synchronization.
    Poison(String),

    /// Error occured in serialization of rust data into values for the
    /// database.
    Serialization(SerializationError),

    /// Error due to wrong usage of API.
    Usage(String),
}

impl HdbError {
    #[doc(hidden)]
    pub fn impl_<S: AsRef<str>>(s: S) -> HdbError {
        HdbError::Impl(s.as_ref().to_owned())
    }
    #[doc(hidden)]
    pub fn usage_<S: AsRef<str>>(s: S) -> HdbError {
        HdbError::Usage(s.as_ref().to_owned())
    }
}

impl error::Error for HdbError {
    fn description(&self) -> &str {
        match *self {
            HdbError::DbError(_) => "Error from database server",
            HdbError::DbIssue(_) => "Issue on database server",
            HdbError::MultipleDbErrors(_) => "Multiple errors from database server",
            HdbError::Conversion(_) => "Conversion of database type to rust type failed",
            HdbError::Deserialization(ref e) => e.description(),
            HdbError::Cesu8(ref e) => e.description(),
            HdbError::Io(ref e) => e.description(),
            HdbError::Serialization(ref e) => e.description(),
            HdbError::Impl(ref s)
            | HdbError::Evaluation(ref s)
            | HdbError::Usage(ref s)
            | HdbError::Poison(ref s) => s,
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            HdbError::Cesu8(ref e) => Some(e),
            HdbError::Conversion(ref error) => Some(error),
            HdbError::Deserialization(ref error) => Some(error),
            HdbError::Io(ref error) => Some(error),
            HdbError::Serialization(ref error) => Some(error),
            HdbError::Impl(_)
            | HdbError::DbError(_)
            | HdbError::DbIssue(_)
            | HdbError::MultipleDbErrors(_)
            | HdbError::Usage(_)
            | HdbError::Poison(_)
            | HdbError::Evaluation(_) => None,
        }
    }
}

impl fmt::Display for HdbError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            HdbError::Cesu8(ref e) => write!(fmt, "{}", e),
            HdbError::Conversion(ref e) => write!(fmt, "{}", e),
            HdbError::Deserialization(ref error) => write!(fmt, "{:?}", error),
            HdbError::Io(ref error) => write!(fmt, "{:?}", error),
            HdbError::Impl(ref error) => write!(fmt, "{:?}", error),
            HdbError::Serialization(ref error) => write!(fmt, "{:?}", error),
            HdbError::Evaluation(ref s)
            | HdbError::Usage(ref s)
            | HdbError::Poison(ref s)
            | HdbError::DbIssue(ref s) => write!(fmt, "{:?}", s),
            HdbError::DbError(ref se) => write!(fmt, "{:?}", se),
            HdbError::MultipleDbErrors(ref vec) => write!(fmt, "{:?}", vec[0]),
        }
    }
}

impl From<ConversionError> for HdbError {
    fn from(error: ConversionError) -> HdbError {
        HdbError::Conversion(error)
    }
}

impl From<DeserializationError> for HdbError {
    fn from(error: DeserializationError) -> HdbError {
        HdbError::Deserialization(error)
    }
}

impl From<SerializationError> for HdbError {
    fn from(error: SerializationError) -> HdbError {
        HdbError::Serialization(error)
    }
}

impl From<String> for HdbError {
    fn from(s: String) -> HdbError {
        HdbError::Usage(s)
    }
}

impl From<io::Error> for HdbError {
    fn from(error: io::Error) -> HdbError {
        HdbError::Io(error)
    }
}

impl From<fmt::Error> for HdbError {
    fn from(error: fmt::Error) -> HdbError {
        HdbError::Usage(error.description().to_owned())
    }
}

impl From<Cesu8DecodingError> for HdbError {
    fn from(error: Cesu8DecodingError) -> HdbError {
        HdbError::Cesu8(error)
    }
}

impl<'a> From<sync::PoisonError<sync::MutexGuard<'a, ConnectionCore>>> for HdbError {
    fn from(error: sync::PoisonError<sync::MutexGuard<'a, ConnectionCore>>) -> HdbError {
        HdbError::Poison(error.description().to_owned())
    }
}

impl<'a> From<sync::PoisonError<sync::MutexGuard<'a, ResultSetCore>>> for HdbError {
    fn from(error: sync::PoisonError<sync::MutexGuard<'a, ResultSetCore>>) -> HdbError {
        HdbError::Poison(error.description().to_owned())
    }
}