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
//! `Error` and `Result` types arising out of MongoDB operations.

use std::fmt;
use std::error;
use std::result;
use std::ops::Deref;
use std::borrow::Cow;
use backtrace::Backtrace;
use bson;
use mongodb;

/// Slightly augmented trait for backtrace-able errors.
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
pub trait ErrorExt: error::Error {
    /// Similar to `std::error::Error::cause()`, but with richer type info.
    fn reason(&self) -> Option<&ErrorExt> {
        None
    }

    /// Returns the deepest possible backtrace, if any.
    fn backtrace(&self) -> Option<&Backtrace> {
        None
    }

    /// Until subtrait coercions are implemented, this helper method
    /// should return the receiver as an `&std::error::Error` trait object.
    fn as_std_error(&self) -> &error::Error;
}

/// A trait for conveniently propagating errors up the call stack.
pub trait ResultExt<T>: Sized {
    /// If this `Result` is an `Err`, then prepend the specified error
    /// to the front of the linked list of causes.
    fn chain<M: ErrMsg>(self, message: M) -> Result<T>;
}

/// Values that can act as or generate an error message.
pub trait ErrMsg: Sized {
    /// Convert the value to an error message.
    fn into_message(self) -> Cow<'static, str>;
}

/// Type alias for a `Result` containing an Avocado `Error`.
pub type Result<T> = result::Result<T, Error>;

impl<T, E> ResultExt<T> for result::Result<T, E> where E: ErrorExt + 'static {
    fn chain<M: ErrMsg>(self, message: M) -> Result<T> {
        self.map_err(|cause| {
            let message = message.into_message();
            let backtrace = if cause.backtrace().is_none() {
                Some(Backtrace::new())
            } else {
                None
            };
            let cause: Option<Box<ErrorExt>> = Some(Box::new(cause));
            Error { message, cause, backtrace }
        })
    }
}

/// Blanket `impl ErrMsg` for string literals.
impl ErrMsg for &'static str {
    fn into_message(self) -> Cow<'static, str> {
        Cow::Borrowed(self)
    }
}

/// Blanket `impl ErrMsg` for error message formatting functions.
impl<F> ErrMsg for F where F: FnOnce() -> String {
    fn into_message(self) -> Cow<'static, str> {
        Cow::Owned(self())
    }
}

/// The central error type for Avocado.
#[derive(Debug)]
pub struct Error {
    /// The human-readable description.
    message: Cow<'static, str>,
    /// The underlying error, if any.
    cause: Option<Box<ErrorExt>>,
    /// The backtrace, if any.
    backtrace: Option<Backtrace>,
}

impl Error {
    /// Creates an error with the specified message, no cause, and a backtrace.
    pub fn new<S>(message: S) -> Self where S: Into<Cow<'static, str>> {
        Error {
            message: message.into(),
            cause: None,
            backtrace: Some(Backtrace::new()),
        }
    }

    /// Creates an error with the specified message and cause. If the cause has
    /// no backtrace, this method will create it and add it to the new instance.
    pub fn with_cause<S, E>(message: S, cause: E) -> Self
        where S: Into<Cow<'static, str>>,
              E: ErrorExt + 'static
    {
        let message = message.into();
        let backtrace = if cause.backtrace().is_none() {
            Some(Backtrace::new())
        } else {
            None
        };
        let cause: Option<Box<ErrorExt>> = Some(Box::new(cause));

        Error { message, cause, backtrace }
    }
}

impl ErrorExt for Error {
    fn reason(&self) -> Option<&ErrorExt> {
        self.cause.as_ref().map(Deref::deref)
    }

    #[cfg_attr(feature = "cargo-clippy", allow(or_fun_call))]
    fn backtrace(&self) -> Option<&Backtrace> {
        self.reason().and_then(ErrorExt::backtrace).or(self.backtrace.as_ref())
    }

    fn as_std_error(&self) -> &error::Error {
        self
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.message)?;

        if let Some(cause) = self.cause.as_ref() {
            write!(f, ", caused by: {}", cause)?
        }

        if let Some(backtrace) = self.backtrace.as_ref() {
            write!(f, "; {:#?}", backtrace)?
        }

        Ok(())
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        &self.message
    }

    fn cause(&self) -> Option<&error::Error> {
        self.reason().map(ErrorExt::as_std_error)
    }
}

/// Implementing `ErrorExt` and `From` boilerplate.
macro_rules! impl_error_type {
    ($ty:path, $message:expr) => {
        impl From<$ty> for Error {
            fn from(error: $ty) -> Self {
                Self::with_cause($message, error)
            }
        }

        impl ErrorExt for $ty {
            fn as_std_error(&self) -> &error::Error {
                self
            }
        }
    }
}

impl_error_type! { bson::EncoderError,     "BSON encoding error" }
impl_error_type! { bson::DecoderError,     "BSON decoding error" }
impl_error_type! { bson::ValueAccessError, "missing or ill-typed BSON value" }

impl_error_type! { mongodb::Error,                           "MongoDB error" }
impl_error_type! { mongodb::coll::error::WriteException,     "MongoDB write exception" }
impl_error_type! { mongodb::coll::error::BulkWriteException, "MongoDB bulk write exception" }