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
use std::fmt;
use wasm_bindgen::{JsCast, JsValue};
pub struct JsError {
pub name: String,
pub message: String,
js_to_string: String,
}
impl fmt::Debug for JsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JsError")
.field("name", &self.name)
.field("message", &self.message)
.finish()
}
}
impl fmt::Display for JsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.js_to_string)
}
}
#[derive(Debug, thiserror::Error)]
pub enum StorageError {
#[error("{0}")]
SerdeError(#[from] serde_json::Error),
#[error("key {0} not found")]
KeyNotFound(String),
#[error("{0}")]
JsError(JsError),
}
pub(crate) fn js_to_error(js_value: JsValue) -> StorageError {
match js_value.dyn_into::<js_sys::Error>() {
Ok(error) => StorageError::JsError(JsError {
name: String::from(error.name()),
message: String::from(error.message()),
js_to_string: String::from(error.to_string()),
}),
Err(_) => unreachable!("JsValue passed is not an Error type - this is a bug"),
}
}