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
use std::fmt;
use wasm_bindgen::JsValue;
#[derive(Debug)]
pub enum ClientError {
JsValueError(JsValue),
WebSocketError(JsValue),
WebGlError(String),
ResourceError(String),
EngineError(String),
}
fn jsvalue_to_string(v: &JsValue) -> String {
v.as_string()
.or_else(|| {
js_sys::Reflect::get(v, &JsValue::from_str("description"))
.ok()
.and_then(|x| x.as_string())
})
.unwrap_or_else(|| format!("error: {:?}", v))
}
impl std::fmt::Display for ClientError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ClientError::JsValueError(e) | ClientError::WebSocketError(e) => {
write!(f, "{}", jsvalue_to_string(e))
}
ClientError::ResourceError(e)
| ClientError::WebGlError(e)
| ClientError::EngineError(e) => write!(f, "{}", e),
}
}
}
macro_rules! derive_from {
($type:ty, $kind:ident) => {
impl From<$type> for ClientError {
fn from(error: $type) -> Self {
ClientError::$kind(format!("{}", error))
}
}
};
}
impl From<JsValue> for ClientError {
fn from(error: JsValue) -> Self {
ClientError::JsValueError(error)
}
}
derive_from!(rask_engine::error::EngineError, EngineError);