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
use std::fmt;

use rutie::{self, Exception, Object};

pub enum ErrorKind {
    Message(String),
    RutieException(rutie::AnyException),
    NotImplemented(&'static str),
}
use self::ErrorKind::*;

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Message(ref msg) => write!(f, "{}", msg),
            RutieException(ref exception) => {
                let inspect = exception.protect_send("inspect", &[]);
                let msg = match inspect {
                    Ok(inspect) => inspect
                        .try_convert_to::<rutie::RString>()
                        .map(|rstring| rstring.to_string())
                        .unwrap_or_else(|_| "unexpected inspect result".to_owned()),
                    Err(_) => "error calling inspect".to_owned(),
                };
                write!(f, "{}", msg)
            }
            NotImplemented(ref description) => write!(f, "{}", description),
        }
    }
}

impl fmt::Debug for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

pub struct Error {
    kind: ErrorKind,
    context: Vec<String>,
}

impl Error {
    pub fn chain_context<F, S>(mut self, func: F) -> Self
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        self.context.push(func().into());
        self
    }

    fn describe_context(&self) -> String {
        if self.context.is_empty() {
            "".to_owned()
        } else {
            format!("\nContext from Rust:\n - {}", self.context.join("\n - "))
        }
    }
}

impl ::std::error::Error for Error {
    fn description(&self) -> &str {
        match self.kind {
            Message(_) => "Generic Error",
            RutieException(_) => "Rutie Exception",
            NotImplemented(description) => description,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}\n{}", self.kind, self.describe_context())
    }
}

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

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Error {
            kind,
            context: vec![],
        }
    }
}

impl<'a> From<&'a str> for Error {
    fn from(string: &'a str) -> Self {
        ErrorKind::Message(string.into()).into()
    }
}

impl From<String> for Error {
    fn from(string: String) -> Self {
        ErrorKind::Message(string).into()
    }
}
impl From<rutie::AnyException> for Error {
    fn from(exception: rutie::AnyException) -> Self {
        ErrorKind::RutieException(exception).into()
    }
}

// Many Rutie methods return `Result<AnyObject, AnyObject>` - we should try
// treat the error `AnyObject` as an `AnyException`.
impl From<rutie::AnyObject> for Error {
    fn from(obj: rutie::AnyObject) -> Self {
        obj.try_convert_to::<rutie::AnyException>()
            .map(Into::into)
            .unwrap_or_else(|_| "Error coercing AnyObject into AnyException".into())
    }
}

impl serde::de::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: fmt::Display,
    {
        format!("{}", msg).into()
    }
}

impl serde::ser::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: fmt::Display,
    {
        format!("{}", msg).into()
    }
}

pub trait IntoException {
    fn into_exception(self, default_class: rutie::Class) -> rutie::AnyException;
}

impl IntoException for Error {
    fn into_exception(self, default_class: rutie::Class) -> rutie::AnyException {
        match self.kind {
            RutieException(ref exception) => {
                let msg = format!("{}{}", exception.message(), self.describe_context());
                exception.exception(Some(&msg))
            }
            _ => {
                let msg = format!("{}", self);
                let obj =
                    default_class.new_instance(&[rutie::RString::new_utf8(&msg).to_any_object()]);
                rutie::AnyException::from(obj.value())
            }
        }
    }
}

pub type Result<T> = ::std::result::Result<T, Error>;

/// This extension trait allows callers to call `.chain_context` to add extra
/// context to errors, in the same way as error-chain's `.chain_err`. The
/// provided context will be passed to Ruby with any Exception.
pub trait ResultExt {
    fn chain_context<F, S>(self, func: F) -> Self
    where
        F: FnOnce() -> S,
        S: Into<String>;
}

impl<T> ResultExt for Result<T> {
    fn chain_context<F, S>(self, func: F) -> Self
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        match self {
            Ok(ok) => Ok(ok),
            Err(err) => Err(err.chain_context(func)),
        }
    }
}