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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Error enum used by the `workflow_wasm` crate.

use crate::jserror::JsErrorData;
use thiserror::Error;
use wasm_bindgen::prelude::*;

#[derive(Error, Debug, Clone)]
pub enum Error {
    #[error("{0}")]
    Custom(String),

    #[error("type error: {0}")]
    WrongType(String),

    #[error("size error: {0}")]
    WrongSize(String),

    #[error("missing property `{0}`")]
    MissingProperty(String),

    #[error("error accessing property `{0}`")]
    PropertyAccess(String),

    #[error("{0}")]
    Bounds(String),

    #[error("{0}")]
    Convert(String),

    #[error("hex string must have an even number of characters: `{0}`")]
    HexStringNotEven(String),

    #[error(transparent)]
    FasterHex(#[from] faster_hex::Error),

    #[error(transparent)]
    JsValue(JsErrorData),

    #[error("WASM ABI: {0}")]
    Abi(String),

    #[error("supplied argument is not an object")]
    NotAnObject,

    #[error("supplied object is not a WASM ABI pointer")]
    NotWasmAbiPointer,

    #[error("supplied object is not a WASM ABI pointer for class `{0}`")]
    NotWasmAbiPointerForClass(String),

    #[error("supplied argument is not an object of class type `{0}`")]
    NotAnObjectOfClass(String),

    #[error("unable to obtain object constructor (for expected class `{0}`)")]
    NoConstructorOfClass(String),

    #[error("unable to obtain object constructor name (for expected class `{0}`)")]
    UnableToObtainConstructorName(String),

    #[error("object constructor `{0}` does not match expected class `{1}`")]
    ClassConstructorMatch(String, String),
}

impl From<Error> for JsValue {
    fn from(err: Error) -> Self {
        JsValue::from_str(&err.to_string())
    }
}

impl From<JsValue> for Error {
    fn from(error: JsValue) -> Self {
        Error::JsValue(error.into())
    }
}

impl Error {
    pub fn custom<S: ToString>(msg: S) -> Self {
        Self::Custom(msg.to_string())
    }

    pub fn convert<S: std::fmt::Display>(msg: S) -> Self {
        Self::Convert(msg.to_string())
    }
}