1use thiserror::Error;
2use wasm_bindgen::JsValue;
3use workflow_core::channel::{RecvError, SendError, TrySendError};
4use workflow_core::sendable::Sendable;
5
6#[derive(Debug, Error)]
8pub enum Error {
9 #[error("{0}")]
12 Custom(String),
13
14 #[error(transparent)]
16 IoError(#[from] std::io::Error),
17
18 #[error("Channel send() error")]
20 SendError,
21
22 #[error("Channel try_send() error")]
24 TrySendError,
25
26 #[error("Channel recv() error")]
28 RecvError,
29
30 #[error("No such key in storage {0}")]
32 MissingKey(String),
33
34 #[error("Key already exists in storage {0}")]
36 KeyExists(String),
37}
38
39impl Error {
40 pub fn custom<T: Into<String>>(msg: T) -> Self {
42 Error::Custom(msg.into())
43 }
44}
45
46impl From<JsValue> for Error {
47 fn from(err: JsValue) -> Self {
48 Self::Custom(err.as_string().unwrap())
49 }
50}
51
52impl From<Sendable<JsValue>> for Error {
53 fn from(err: Sendable<JsValue>) -> Self {
54 Self::Custom(err.0.as_string().unwrap())
55 }
56}
57
58impl From<Error> for JsValue {
59 fn from(err: Error) -> Self {
60 JsValue::from_str(&err.to_string())
61 }
62}
63
64impl From<String> for Error {
65 fn from(err: String) -> Self {
66 Error::Custom(err)
67 }
68}
69
70impl<T> From<SendError<T>> for Error {
71 fn from(_: SendError<T>) -> Self {
72 Error::SendError
73 }
74}
75
76impl From<RecvError> for Error {
77 fn from(_: RecvError) -> Self {
78 Error::RecvError
79 }
80}
81
82impl<T> From<TrySendError<T>> for Error {
83 fn from(_: TrySendError<T>) -> Self {
84 Error::TrySendError
85 }
86}