1use crate::jserror::JsErrorData;
4use thiserror::Error;
5use wasm_bindgen::prelude::*;
6
7#[derive(Error, Debug, Clone)]
9pub enum Error {
10 #[error("{0}")]
12 Custom(String),
13
14 #[error("type error: {0}")]
16 WrongType(String),
17
18 #[error("size error: {0}")]
20 WrongSize(String),
21
22 #[error("missing property `{0}`")]
24 MissingProperty(String),
25
26 #[error("error accessing property `{0}`")]
28 PropertyAccess(String),
29
30 #[error("{0}")]
32 Bounds(String),
33
34 #[error("{0}")]
36 Convert(String),
37
38 #[error("hex string must have an even number of characters: `{0}`")]
40 HexStringNotEven(String),
41
42 #[error(transparent)]
44 FasterHex(#[from] faster_hex::Error),
45
46 #[error(transparent)]
48 JsValue(JsErrorData),
49
50 #[error("WASM ABI: {0}")]
52 Abi(String),
53
54 #[error("supplied argument is not an object")]
56 NotAnObject,
57
58 #[error("supplied object is not a WASM ABI pointer")]
60 NotWasmAbiPointer,
61
62 #[error("supplied object is not a WASM ABI pointer for class `{0}`")]
64 NotWasmAbiPointerForClass(String),
65
66 #[error("supplied argument is not an object of class type `{0}`")]
68 NotAnObjectOfClass(String),
69
70 #[error("unable to obtain object constructor (for expected class `{0}`)")]
72 NoConstructorOfClass(String),
73
74 #[error("unable to obtain object constructor name (for expected class `{0}`)")]
76 UnableToObtainConstructorName(String),
77
78 #[error("object constructor `{0}` does not match expected class `{1}`")]
80 ClassConstructorMatch(String, String),
81}
82
83impl From<Error> for JsValue {
84 fn from(err: Error) -> Self {
85 JsValue::from_str(&err.to_string())
86 }
87}
88
89impl From<JsValue> for Error {
90 fn from(error: JsValue) -> Self {
91 Error::JsValue(error.into())
92 }
93}
94
95impl Error {
96 pub fn custom<S: ToString>(msg: S) -> Self {
98 Self::Custom(msg.to_string())
99 }
100
101 pub fn convert<S: std::fmt::Display>(msg: S) -> Self {
103 Self::Convert(msg.to_string())
104 }
105}