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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Error enum used by the `workflow_wasm` crate.
use crate::jserror::JsErrorData;
use thiserror::Error;
use wasm_bindgen::prelude::*;
/// Errors produced by the `workflow_wasm` crate.
#[derive(Error, Debug, Clone)]
pub enum Error {
/// Custom error carrying an arbitrary message.
#[error("{0}")]
Custom(String),
/// A value did not have the expected JavaScript type.
#[error("type error: {0}")]
WrongType(String),
/// A value or buffer did not have the expected size.
#[error("size error: {0}")]
WrongSize(String),
/// A required object property was not present.
#[error("missing property `{0}`")]
MissingProperty(String),
/// An error occurred while accessing an object property.
#[error("error accessing property `{0}`")]
PropertyAccess(String),
/// A numeric value fell outside of its permitted range.
#[error("{0}")]
Bounds(String),
/// A value could not be converted to the requested type.
#[error("{0}")]
Convert(String),
/// A hex string had an odd number of characters and cannot be decoded.
#[error("hex string must have an even number of characters: `{0}`")]
HexStringNotEven(String),
/// Error propagated from the `faster_hex` decoding library.
#[error(transparent)]
FasterHex(#[from] faster_hex::Error),
/// Error originating from a JavaScript [`JsValue`].
#[error(transparent)]
JsValue(JsErrorData),
/// Error relating to the WASM ABI representation.
#[error("WASM ABI: {0}")]
Abi(String),
/// The supplied argument was not a JavaScript object.
#[error("supplied argument is not an object")]
NotAnObject,
/// The supplied object did not carry a WASM ABI pointer.
#[error("supplied object is not a WASM ABI pointer")]
NotWasmAbiPointer,
/// The supplied object did not carry a WASM ABI pointer for the named class.
#[error("supplied object is not a WASM ABI pointer for class `{0}`")]
NotWasmAbiPointerForClass(String),
/// The supplied argument was not an object of the named class type.
#[error("supplied argument is not an object of class type `{0}`")]
NotAnObjectOfClass(String),
/// The object's constructor could not be obtained for the expected class.
#[error("unable to obtain object constructor (for expected class `{0}`)")]
NoConstructorOfClass(String),
/// The object's constructor name could not be obtained for the expected class.
#[error("unable to obtain object constructor name (for expected class `{0}`)")]
UnableToObtainConstructorName(String),
/// The object's constructor name did not match the expected class name.
#[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 {
/// Construct a [`Error::Custom`] error from any stringifiable message.
pub fn custom<S: ToString>(msg: S) -> Self {
Self::Custom(msg.to_string())
}
/// Construct a [`Error::Convert`] error from a displayable conversion message.
pub fn convert<S: std::fmt::Display>(msg: S) -> Self {
Self::Convert(msg.to_string())
}
}