Skip to main content

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/// Errors produced by `workflow-chrome` operations.
7#[derive(Debug, Error)]
8pub enum Error {
9    /// A custom error carrying an arbitrary message, also used to wrap
10    /// JavaScript (`JsValue`) errors.
11    #[error("{0}")]
12    Custom(String),
13
14    /// An underlying I/O error.
15    #[error(transparent)]
16    IoError(#[from] std::io::Error),
17
18    /// A channel `send()` operation failed.
19    #[error("Channel send() error")]
20    SendError,
21
22    /// A channel `try_send()` operation failed.
23    #[error("Channel try_send() error")]
24    TrySendError,
25
26    /// A channel `recv()` operation failed.
27    #[error("Channel recv() error")]
28    RecvError,
29
30    /// The requested key was not found in storage.
31    #[error("No such key in storage {0}")]
32    MissingKey(String),
33
34    /// A key that already exists in storage was used where it must be absent.
35    #[error("Key already exists in storage {0}")]
36    KeyExists(String),
37}
38
39impl Error {
40    /// Creates a [`Error::Custom`] from any value convertible into a `String`.
41    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}