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
use derive_more::From;
use displaydoc::Display;
use thiserror::Error;
use wasm_bindgen::JsValue;

#[derive(Debug, Display, Error, From)]
pub enum Error {
    /// Child element not found: '{0}'
    #[from(ignore)]
    ChildElementNotFound(&'static str),
    /// DOM element not found: '{0}'
    #[from(ignore)]
    DomElementNotFound(&'static str),
    /// Element ID not found: '{0}'
    #[from(ignore)]
    ElementIdNotFound(&'static str),
    /// First element in collection not found
    FirstElementNotFound,
    /// Failed to get dynamic reference type
    DynRefFailed,
    /// Failed to cast to HTML element
    NotHtmlElement,
    /// Failed to cast to `EventTarget`
    NotEventTarget,
    /// `Document` without document `Element`
    NoDocumentElement,
    /// `Element` type does not have '{0}' value
    #[from(ignore)]
    NoValue(&'static str),
    /// Cannot remote attribute: '{0}'
    #[from(ignore)]
    CannotRemoveAttribute(String),
    /// Selectors Parser Error
    SelectorsParserError,
    /// Other Error
    JsValue(JsValue),
}

impl Into<JsValue> for Error {
    fn into(self) -> JsValue {
        match self {
            Error::JsValue(value) => value,
            _ => JsValue::from_str(&self.to_string()),
        }
    }
}