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
use std::{
borrow::Cow,
fmt::{Debug, Display},
rc::Rc,
};
use derive_more::{Display, From};
use tracerr::{Trace, Traced};
use wasm_bindgen::{prelude::*, JsCast};
pub use medea_macro::JsCaused;
#[cfg(debug_assertions)]
pub fn console_error<M>(msg: M)
where
M: Into<JsValue>,
{
web_sys::console::error_1(&msg.into());
}
pub trait JsCaused {
type Error;
fn name(&self) -> &'static str;
fn js_cause(self) -> Option<Self::Error>;
}
#[derive(Clone, Debug, Display, PartialEq)]
#[display(fmt = "{}: {}", name, message)]
pub struct JsError {
pub name: Cow<'static, str>,
pub message: Cow<'static, str>,
}
impl From<JsValue> for JsError {
fn from(val: JsValue) -> Self {
match val.dyn_into::<js_sys::Error>() {
Ok(err) => Self {
name: Cow::Owned(err.name().into()),
message: Cow::Owned(err.message().into()),
},
Err(val) => match val.as_string() {
Some(reason) => Self {
name: "Unknown JS error".into(),
message: reason.into(),
},
None => Self {
name: "Unknown JS error".into(),
message: format!("{:?}", val).into(),
},
},
}
}
}
impl From<JsError> for js_sys::Error {
fn from(err: JsError) -> Self {
let error = Self::new(&err.message);
error.set_name(&err.name);
error
}
}
#[wasm_bindgen]
#[derive(Clone, Debug, Display)]
#[display(fmt = "{}: {}\n{}", name, message, trace)]
pub struct JasonError {
name: &'static str,
message: String,
trace: Trace,
source: Option<js_sys::Error>,
}
impl JasonError {
pub fn print(&self) {
log::error!("{}", self);
}
}
#[wasm_bindgen]
impl JasonError {
#[must_use]
pub fn name(&self) -> String {
String::from(self.name)
}
#[must_use]
pub fn message(&self) -> String {
self.message.clone()
}
#[must_use]
pub fn trace(&self) -> String {
self.trace.to_string()
}
#[must_use]
pub fn source(&self) -> Option<js_sys::Error> {
Clone::clone(&self.source)
}
}
impl<E: JsCaused + Display> From<(E, Trace)> for JasonError
where
E::Error: Into<js_sys::Error>,
{
fn from((err, trace): (E, Trace)) -> Self {
Self {
name: err.name(),
message: err.to_string(),
trace,
source: err.js_cause().map(Into::into),
}
}
}
impl<E: JsCaused + Display> From<Traced<E>> for JasonError
where
E::Error: Into<js_sys::Error>,
{
fn from(traced: Traced<E>) -> Self {
Self::from(traced.into_parts())
}
}
#[derive(Debug, Display, JsCaused)]
#[display(fmt = "Handler is in detached state.")]
pub struct HandlerDetachedError;
#[derive(Clone, Debug, Display, From)]
#[from(forward)]
pub struct JsonParseError(Rc<serde_json::error::Error>);
impl PartialEq for JsonParseError {
fn eq(&self, other: &Self) -> bool {
self.0.line() == other.0.line()
&& self.0.column() == other.0.column()
&& self.0.classify() == other.0.classify()
}
}