xwt_web/
error.rs

1//! Error type definition.
2
3/// A generic error encapsulating a JS-land value.
4#[derive(Debug)]
5pub struct Error(pub wasm_bindgen::JsValue);
6
7impl std::fmt::Display for Error {
8    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9        use std::fmt::Debug;
10        self.0.fmt(f)
11    }
12}
13
14impl std::error::Error for Error {}
15
16impl From<wasm_bindgen::JsValue> for Error {
17    fn from(value: wasm_bindgen::JsValue) -> Self {
18        Self(value)
19    }
20}
21
22impl From<wasm_bindgen::JsError> for Error {
23    fn from(value: wasm_bindgen::JsError) -> Self {
24        wasm_bindgen::JsValue::from(value).into()
25    }
26}
27
28impl From<web_sys::DomException> for Error {
29    fn from(value: web_sys::DomException) -> Self {
30        wasm_bindgen::JsValue::from(value).into()
31    }
32}
33
34impl From<String> for Error {
35    fn from(value: String) -> Self {
36        wasm_bindgen::JsValue::from(value).into()
37    }
38}