workflow_chrome/
error.rs

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