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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! Error-handling.
//!
//! Types for error-handling in this crate, based on the [`failure`] crate.
//!
//! [`Error`] is the wrapper which implements [`Fail`] and containers the inner
//! [`ErrorKind`] and its [`Context`].
//!
//! [`failure`]: ../../failure/index.html
//! [`Error`]: ./struct.Error.html
//! [`ErrorKind`]: ./enum.ErrorKind.html
//! [`Fail`]: ../../failure/trait.Fail.html
//! [`Context`]: ../../failure/struct.Context.html

use failure::{Backtrace, Context, Fail};
use std::{fmt, fmt::Display, result};

/// Internal [`Result`] type which uses the crate's [`Error`] type.
///
/// [`Error`]: ./struct.Error.html
pub type Result<T> = result::Result<T, Error>;

/// Re-export the [`ResultExt`] trait which adds the [`Context`] methods to
/// [`Result`].
///
/// [`ResultExt`]: ../../failure/trait.ResultExt.html
pub use failure::ResultExt;

/// [`Error`] type for this crate.
///
/// Implements [`Fail`].
///
/// [`Error`]: ./struct.Error.html
/// [`Fail`]: ../../failure/trait.Fail.html
#[derive(Debug)]
pub struct Error {
    /// [`Context`] which contains the [`ErrorKind`].
    ///
    /// [`Context`]: ../../failure/struct.Context.html
    /// [`ErrorKind`]: ./enum.ErrorKind.html
    ctx: Context<ErrorKind>,
}

#[derive(PartialEq, Debug, Fail)]
pub enum ErrorKind {
    /// LogError
    #[fail(display = "Log error: {}", _0)]
    LogError(String),

    /// Generic invalid argument: use when a function is called in a way it
    /// shouldn't be.
    #[fail(display = "Invalid argument: {}", _0)]
    InvalidArgument(String),

    /// Generic invalid operation: use when a function is called while it
    /// shouldn't be.
    #[fail(display = "Invalid operation: {}", _0)]
    InvalidOperation(String),

    /// Generic error: use when an error doesn't fit in the above categories
    /// and you're too lazy to define one properly.
    #[fail(display = "{}", _0)]
    Other(String),

    /// Wraps multiple errors that occurred asynchronously.
    #[fail(display = "Multiple errors occurred. Check the log.")]
    Multiple(Vec<ErrorKind>),

    /// For propagating crossbeam_channel errors.
    #[fail(display = "Inter-thread communication error: {}", _0)]
    ITCError(String),

    /// For propagating ipc_channel errors.
    #[fail(display = "Interprocess communication error: {}", _0)]
    IPCError(String),

    /// For propagating std::io::Error errors.
    #[fail(display = "I/O error: {}", _0)]
    IoError(String, std::io::ErrorKind),

    /// For propagating term::Error errors.
    #[fail(display = "Terminal error: {}", _0)]
    TermError(String, term::Error),
}

/// Shorthand for producing a LogError.
pub fn log_err<T>(s: impl Into<String>) -> Result<T> {
    Err(ErrorKind::LogError(s.into()).into())
}

/// Shorthand for producing a LogError in an or_else function.
pub fn oe_log_err(s: impl Into<String>) -> impl FnOnce() -> Error {
    move || ErrorKind::LogError(s.into()).into()
}

/// Shorthand for producing an invalid argument error.
pub fn inv_arg<T>(s: impl Into<String>) -> Result<T> {
    Err(ErrorKind::InvalidArgument(s.into()).into())
}

/// Shorthand for producing an invalid argument error in an or_else function.
pub fn oe_inv_arg(s: impl Into<String>) -> impl FnOnce() -> Error {
    move || ErrorKind::InvalidArgument(s.into()).into()
}

/// Shorthand for producing an invalid operation error.
pub fn inv_op<T>(s: impl Into<String>) -> Result<T> {
    Err(ErrorKind::InvalidOperation(s.into()).into())
}

/// Shorthand for producing an invalid operation error in an or_else function.
pub fn oe_inv_op(s: impl Into<String>) -> impl FnOnce() -> Error {
    move || ErrorKind::InvalidOperation(s.into()).into()
}

/// Shorthand for producing an error that does not fit in any of the ErrorKind
/// classes.
pub fn err<T>(s: impl Into<String>) -> Result<T> {
    Err(ErrorKind::Other(s.into()).into())
}

/// err() but for or_else() functions.
pub fn oe_err(s: impl Into<String>) -> impl FnOnce() -> Error {
    move || ErrorKind::Other(s.into()).into()
}

impl Fail for Error {
    fn cause(&self) -> Option<&Fail> {
        self.ctx.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.ctx.backtrace()
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.ctx.fmt(f)
    }
}

impl From<ErrorKind> for Error {
    fn from(ctx: ErrorKind) -> Error {
        Error {
            ctx: Context::new(ctx),
        }
    }
}

impl From<Context<String>> for Error {
    fn from(ctx: Context<String>) -> Error {
        Error {
            ctx: ctx.map(ErrorKind::Other),
        }
    }
}

impl From<Context<ErrorKind>> for Error {
    fn from(ctx: Context<ErrorKind>) -> Error {
        Error { ctx }
    }
}

impl From<std::io::Error> for Error {
    fn from(error: std::io::Error) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::IoError(msg, error.kind())),
        }
    }
}

impl From<term::Error> for Error {
    fn from(error: term::Error) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::TermError(msg, error)),
        }
    }
}

impl<T> From<crossbeam_channel::SendError<T>> for Error {
    fn from(error: crossbeam_channel::SendError<T>) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::ITCError(msg)),
        }
    }
}

impl From<ipc_channel::Error> for Error {
    fn from(error: ipc_channel::Error) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::IPCError(msg)),
        }
    }
}

impl From<strum::ParseError> for Error {
    fn from(error: strum::ParseError) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::InvalidArgument(msg)),
        }
    }
}

impl From<serde_yaml::Error> for Error {
    fn from(error: serde_yaml::Error) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::InvalidArgument(msg)),
        }
    }
}

impl From<serde_json::Error> for Error {
    fn from(error: serde_json::Error) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::InvalidArgument(msg)),
        }
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(error: std::str::Utf8Error) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::InvalidArgument(msg)),
        }
    }
}

impl From<std::string::FromUtf8Error> for Error {
    fn from(error: std::string::FromUtf8Error) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::InvalidArgument(msg)),
        }
    }
}

impl From<std::ffi::NulError> for Error {
    fn from(error: std::ffi::NulError) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::InvalidArgument(msg)),
        }
    }
}

impl From<crossbeam_channel::RecvError> for Error {
    fn from(error: crossbeam_channel::RecvError) -> Error {
        let msg = error.to_string();
        Error {
            ctx: Context::new(ErrorKind::ITCError(msg)),
        }
    }
}