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
//! Error Reference

use std::io;
use std::str;
use r2d2::{InitializationError, GetTimeout};
use serde_json::error as json;
use protobuf::ProtobufError;
use prelude::Value;
use scram::Error as ScramError;
use futures::stream::SendError;

quick_error! {
    /// The most generic error message in ReQL
    #[derive(Debug)]
    pub enum Error {
        Compile(descr: String) {
            display("{}", descr)
        }
        Runtime(err: RuntimeError) {
            from()
            description(err.description())
            cause(err)
            display("{:?}", err)
        }
        Driver(err: DriverError) {
            from()
            description(err.description())
            cause(err)
            display("{:?}", err)
        }
    }
}

quick_error! {
/// The parent class of all runtime errors
///
/// All errors on the server unrelated to compilation. Programs may use this to catch any runtime
/// error, but the server will always return a more specific error class.
    #[derive(Debug)]
    pub enum RuntimeError {
        /// The query contains a logical impossibility, such as adding a number to a string.
        QueryLogic(descr: String)
        NonExistence(descr: String)
        ResourceLimit(descr: String)
        User(descr: String)
        Internal(descr: String)
        Timeout(descr: String)
        Availability(err: AvailabilityError) { from() }
        Permission(descr: String)
    }
}

quick_error! {
    /// A server in the cluster is unavailable
    ///
    /// The parent class of `OpFailedError` and `OpIndeterminateError`. Programs may use this
    /// to catch any availability error, but the server will always return one of this class’s
    /// children.
    #[derive(Debug)]
    pub enum AvailabilityError {
        OpFailed(descr: String)
        OpIndeterminate(descr: String)
    }
}

quick_error! {
    /// An error has occurred within the driver
    ///
    /// This may be a driver bug, or it may be an unfulfillable command, such as an unserializable
    /// query.
    #[derive(Debug)]
    pub enum DriverError {
        Auth(descr: String)
        Connection(err: ConnectionError) { from() }
        Response(err: ResponseError) { from() }
        Lock(err: String)
        Json(err: json::Error) { from() }
        Protobuf(err: ProtobufError) { from() }
        Scram(err: ScramError) { from() }
        Other(descr: String)
    }
}

quick_error! {
    /// Connection related errors
    #[derive(Debug)]
    pub enum ConnectionError {
        Initialization(err: InitializationError) { from() }
        Timeout(err: GetTimeout) { from() }
        Io(err: io::Error) { from() }
        Other(descr: String)
    }
}

quick_error! {
    /// Response related errors
    #[derive(Debug)]
    pub enum ResponseError {
        Parse(err: str::Utf8Error) { from() }
        Db(err: Value) { from() }
    }
}

/// Converts from r2d2 error
impl From<InitializationError> for Error {
    fn from(err: InitializationError) -> Error {
        From::from(ConnectionError::Initialization(err))
    }
}

impl From<GetTimeout> for Error {
    fn from(err: GetTimeout) -> Error {
        From::from(ConnectionError::Timeout(err))
    }
}

impl From<ConnectionError> for Error {
    fn from(err: ConnectionError) -> Error {
        From::from(DriverError::Connection(err))
    }
}

impl From<ResponseError> for Error {
    fn from(err: ResponseError) -> Error {
        From::from(DriverError::Response(err))
    }
}

impl<T, E> From<SendError<T, E>> for Error {
    fn from(err: SendError<T, E>) -> Error {
        let err = format!("{:?}", err);
        From::from(DriverError::Other(err))
    }
}

impl From<AvailabilityError> for Error {
    fn from(err: AvailabilityError) -> Error {
        From::from(RuntimeError::Availability(err))
    }
}

/// Converts from IO error
impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        From::from(ConnectionError::Io(err))
    }
}

/// Converts from Utf8Error error
impl From<str::Utf8Error> for Error {
    fn from(err: str::Utf8Error) -> Error {
        From::from(ResponseError::Parse(err))
    }
}

/// Converts from serde_json error
impl From<json::Error> for Error {
    fn from(err: json::Error) -> Error {
        From::from(DriverError::Json(err))
    }
}

impl From<ProtobufError> for Error {
    fn from(err: ProtobufError) -> Error {
        From::from(DriverError::Protobuf(err))
    }
}

impl From<ScramError> for Error {
    fn from(err: ScramError) -> Error {
        From::from(DriverError::Scram(err))
    }
}