workflow_wasm/
error.rs

1//! Error enum used by the `workflow_wasm` crate.
2
3use crate::jserror::JsErrorData;
4use thiserror::Error;
5use wasm_bindgen::prelude::*;
6
7#[derive(Error, Debug, Clone)]
8pub enum Error {
9    #[error("{0}")]
10    Custom(String),
11
12    #[error("type error: {0}")]
13    WrongType(String),
14
15    #[error("size error: {0}")]
16    WrongSize(String),
17
18    #[error("missing property `{0}`")]
19    MissingProperty(String),
20
21    #[error("error accessing property `{0}`")]
22    PropertyAccess(String),
23
24    #[error("{0}")]
25    Bounds(String),
26
27    #[error("{0}")]
28    Convert(String),
29
30    #[error("hex string must have an even number of characters: `{0}`")]
31    HexStringNotEven(String),
32
33    #[error(transparent)]
34    FasterHex(#[from] faster_hex::Error),
35
36    #[error(transparent)]
37    JsValue(JsErrorData),
38
39    #[error("WASM ABI: {0}")]
40    Abi(String),
41
42    #[error("supplied argument is not an object")]
43    NotAnObject,
44
45    #[error("supplied object is not a WASM ABI pointer")]
46    NotWasmAbiPointer,
47
48    #[error("supplied object is not a WASM ABI pointer for class `{0}`")]
49    NotWasmAbiPointerForClass(String),
50
51    #[error("supplied argument is not an object of class type `{0}`")]
52    NotAnObjectOfClass(String),
53
54    #[error("unable to obtain object constructor (for expected class `{0}`)")]
55    NoConstructorOfClass(String),
56
57    #[error("unable to obtain object constructor name (for expected class `{0}`)")]
58    UnableToObtainConstructorName(String),
59
60    #[error("object constructor `{0}` does not match expected class `{1}`")]
61    ClassConstructorMatch(String, String),
62}
63
64impl From<Error> for JsValue {
65    fn from(err: Error) -> Self {
66        JsValue::from_str(&err.to_string())
67    }
68}
69
70impl From<JsValue> for Error {
71    fn from(error: JsValue) -> Self {
72        Error::JsValue(error.into())
73    }
74}
75
76impl Error {
77    pub fn custom<S: ToString>(msg: S) -> Self {
78        Self::Custom(msg.to_string())
79    }
80
81    pub fn convert<S: std::fmt::Display>(msg: S) -> Self {
82        Self::Convert(msg.to_string())
83    }
84}