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
use crate::{
binding::exception,
types::{Value, ValueType},
AnyObject, Class, Exception, NilClass, Object, TryConvert, VerifiedObject,
};
use std::{
borrow::Borrow,
fmt,
fmt::{Display, Formatter},
ops::Deref,
};
pub struct AnyException {
value: Value,
}
impl AnyException {
/// Creates an exception of `class` with `message`, without looking the
/// class up by name (`rb_exc_new_str`).
///
/// Unlike [`Exception::new`](trait.Exception.html#method.new), this
/// cannot fail on an unknown class name.
///
/// # Examples
///
/// ```
/// use rutie::{AnyException, Class, Exception, Object, VM};
/// # VM::init();
///
/// let error = AnyException::from_class(&Class::key_error(), "no such key");
///
/// assert!(Class::key_error().case_equals(&error));
/// assert_eq!(error.message(), "no such key");
///
/// let result = VM::protect(|| {
/// VM::raise_ex(AnyException::from_class(&Class::argument_error(), "bad"));
/// rutie::NilClass::new().into()
/// });
/// assert!(result.is_err());
/// assert_eq!(VM::error_pop().unwrap().message(), "bad");
/// ```
pub fn from_class(class: &Class, message: &str) -> Self {
AnyException::from(exception::new(class.value(), message))
}
/// Creates the `SystemCallError` subclass (`Errno::*`) for the OS error
/// number `errno`, with `message` added to its description
/// (`rb_syserr_new`).
///
/// # Examples
///
/// ```
/// use rutie::{AnyException, Class, Exception, Object, VM};
/// # VM::init();
///
/// let error = AnyException::from_errno(2, "config.yml");
///
/// assert!(Class::from_existing("Errno").get_nested_class("ENOENT").case_equals(&error));
/// assert!(error.message().contains("config.yml"));
/// ```
pub fn from_errno(errno: i32, message: &str) -> Self {
AnyException::from(exception::syserr_new(errno, message))
}
/// Creates the `SystemCallError` subclass for a Rust `std::io::Error`
/// that came from the OS, or a plain `IOError` for any other kind of
/// error, with `message` added to its description.
///
/// # Examples
///
/// ```
/// use rutie::{AnyException, Class, Exception, Object, VM};
/// use std::fs::File;
/// # VM::init();
///
/// let io_error = File::open("/no/such/file/for/rutie").unwrap_err();
/// let error = AnyException::from_io_error(&io_error, "/no/such/file/for/rutie");
///
/// assert!(Class::system_call_error().case_equals(&error));
///
/// let custom = std::io::Error::new(std::io::ErrorKind::Other, "custom");
/// let error = AnyException::from_io_error(&custom, "while reading");
///
/// assert!(Class::io_error().case_equals(&error));
/// assert!(error.message().contains("custom"));
/// ```
pub fn from_io_error(error: &std::io::Error, message: &str) -> Self {
match error.raw_os_error() {
Some(errno) => AnyException::from_errno(errno, message),
None => {
let message = format!("{} - {}", error, message);
AnyException::from_class(&Class::io_error(), &message)
}
}
}
}
impl From<Value> for AnyException {
fn from(value: Value) -> Self {
AnyException { value }
}
}
impl Into<Value> for AnyException {
fn into(self) -> Value {
self.value
}
}
impl Into<AnyObject> for AnyException {
fn into(self) -> AnyObject {
AnyObject::from(self.value)
}
}
impl Borrow<Value> for AnyException {
fn borrow(&self) -> &Value {
&self.value
}
}
impl AsRef<Value> for AnyException {
fn as_ref(&self) -> &Value {
&self.value
}
}
impl AsRef<AnyException> for AnyException {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl Object for AnyException {
#[inline]
fn value(&self) -> Value {
self.value
}
}
impl Deref for AnyException {
type Target = Value;
fn deref(&self) -> &Value {
&self.value
}
}
impl Exception for AnyException {}
impl TryConvert<AnyObject> for AnyException {
type Nil = NilClass;
fn try_convert(obj: AnyObject) -> Result<Self, NilClass> {
obj.try_convert_to::<AnyException>()
.map_err(|_| NilClass::new())
}
}
impl VerifiedObject for AnyException {
fn is_correct_type<T: Object>(object: &T) -> bool {
Class::exception().case_equals(object)
}
fn error_message() -> &'static str {
"Error converting to AnyException"
}
}
impl Display for AnyException {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.inspect())
}
}
impl fmt::Debug for AnyException {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.inspect())
}
}
impl PartialEq for AnyException {
fn eq(&self, other: &Self) -> bool {
self.equals(other)
}
}
#[cfg(test)]
mod tests {
use crate::{AnyException, Class, Exception, Object, VM};
#[test]
fn test_exception_constructors() {
crate::on_ruby_thread(|| {
let error = AnyException::from_class(&Class::runtime_error(), "100% %s");
assert_eq!(error.message(), "100% %s");
assert!(error.try_convert_to::<AnyException>().is_ok());
let enoent = AnyException::from_errno(2, "path");
assert!(Class::system_call_error().case_equals(&enoent));
let errno = unsafe { enoent.send("errno", &[]) };
assert_eq!(errno.try_convert_to::<crate::Fixnum>().unwrap().to_i64(), 2);
// An unknown errno still gives a SystemCallError.
let unknown = AnyException::from_errno(99_999, "odd");
assert!(Class::system_call_error().case_equals(&unknown));
let io = std::io::Error::from_raw_os_error(13);
let eacces = AnyException::from_io_error(&io, "secret");
assert!(Class::from_path("Errno::EACCES")
.unwrap()
.case_equals(&eacces));
// Raised and rescued from Ruby like any exception.
let result = VM::protect(|| {
VM::raise_ex(AnyException::from_class(&Class::type_error(), "typed"));
crate::NilClass::new().into()
});
assert!(result.is_err());
let raised = VM::error_pop().unwrap();
assert!(Class::type_error().case_equals(&raised));
assert!(raised.backtrace().is_some());
});
}
}