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
use webcore::value::Reference;
use webcore::try_from::TryInto;
use webcore::reference_type::ReferenceType;
pub trait IError: ReferenceType {
#[inline]
fn message( &self ) -> String {
js!(
return @{self.as_ref()}.message;
).try_into().unwrap()
}
#[inline]
fn name( &self ) -> String {
js!(
return @{self.as_ref()}.name;
).try_into().unwrap()
}
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "Error")]
pub struct Error( Reference );
impl Error {
#[inline]
pub fn new( description: &str ) -> Self {
js!( return new Error( @{description} ); ).try_into().unwrap()
}
}
impl IError for Error {}
error_boilerplate! { Error }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_error() {
use ::std::fmt::Write;
let error: Error = js!(
return new Error("foo");
).try_into().unwrap();
assert_eq!(error.name(), "Error");
assert_eq!(error.message(), "foo");
let mut text = String::new();
write!(&mut text, "{}", error).unwrap();
assert_eq!(&text, "Error: foo");
assert_eq!(::std::error::Error::description(&error), "Error");
}
}