Skip to main content

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/// Errors produced by the `workflow_wasm` crate.
8#[derive(Error, Debug, Clone)]
9pub enum Error {
10    /// Custom error carrying an arbitrary message.
11    #[error("{0}")]
12    Custom(String),
13
14    /// A value did not have the expected JavaScript type.
15    #[error("type error: {0}")]
16    WrongType(String),
17
18    /// A value or buffer did not have the expected size.
19    #[error("size error: {0}")]
20    WrongSize(String),
21
22    /// A required object property was not present.
23    #[error("missing property `{0}`")]
24    MissingProperty(String),
25
26    /// An error occurred while accessing an object property.
27    #[error("error accessing property `{0}`")]
28    PropertyAccess(String),
29
30    /// A numeric value fell outside of its permitted range.
31    #[error("{0}")]
32    Bounds(String),
33
34    /// A value could not be converted to the requested type.
35    #[error("{0}")]
36    Convert(String),
37
38    /// A hex string had an odd number of characters and cannot be decoded.
39    #[error("hex string must have an even number of characters: `{0}`")]
40    HexStringNotEven(String),
41
42    /// Error propagated from the `faster_hex` decoding library.
43    #[error(transparent)]
44    FasterHex(#[from] faster_hex::Error),
45
46    /// Error originating from a JavaScript [`JsValue`].
47    #[error(transparent)]
48    JsValue(JsErrorData),
49
50    /// Error relating to the WASM ABI representation.
51    #[error("WASM ABI: {0}")]
52    Abi(String),
53
54    /// The supplied argument was not a JavaScript object.
55    #[error("supplied argument is not an object")]
56    NotAnObject,
57
58    /// The supplied object did not carry a WASM ABI pointer.
59    #[error("supplied object is not a WASM ABI pointer")]
60    NotWasmAbiPointer,
61
62    /// The supplied object did not carry a WASM ABI pointer for the named class.
63    #[error("supplied object is not a WASM ABI pointer for class `{0}`")]
64    NotWasmAbiPointerForClass(String),
65
66    /// The supplied argument was not an object of the named class type.
67    #[error("supplied argument is not an object of class type `{0}`")]
68    NotAnObjectOfClass(String),
69
70    /// The object's constructor could not be obtained for the expected class.
71    #[error("unable to obtain object constructor (for expected class `{0}`)")]
72    NoConstructorOfClass(String),
73
74    /// The object's constructor name could not be obtained for the expected class.
75    #[error("unable to obtain object constructor name (for expected class `{0}`)")]
76    UnableToObtainConstructorName(String),
77
78    /// The object's constructor name did not match the expected class name.
79    #[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    /// Construct a [`Error::Custom`] error from any stringifiable message.
97    pub fn custom<S: ToString>(msg: S) -> Self {
98        Self::Custom(msg.to_string())
99    }
100
101    /// Construct a [`Error::Convert`] error from a displayable conversion message.
102    pub fn convert<S: std::fmt::Display>(msg: S) -> Self {
103        Self::Convert(msg.to_string())
104    }
105}