workflow_websocket/client/
error.rs

1use std::sync::PoisonError;
2use thiserror::Error;
3// use tokio::sync::broadcast::error;
4use wasm_bindgen::JsValue;
5use workflow_core::channel::*;
6use workflow_core::sendable::*;
7use workflow_wasm::printable::*;
8
9#[derive(Error, Debug)]
10pub enum Error {
11    #[error("{0}")]
12    Custom(String),
13
14    #[error("{0}")]
15    JsValue(Sendable<Printable>),
16
17    #[error("PoisonError")]
18    PoisonError,
19
20    #[error("Missing WebSocket URL (must be supplied in constructor or the connect() method)")]
21    MissingUrl,
22
23    #[error("WebSocket URL must start with ws:// or wss:// - supplied argument is:`{0}`")]
24    AddressSchema(String),
25
26    #[error("Invalid message type")]
27    InvalidMessageType,
28
29    #[error("InvalidState")]
30    InvalidState(u16),
31
32    #[error("DataEncoding")]
33    DataEncoding,
34
35    #[error("DataType")]
36    DataType,
37
38    #[error("WebSocket connection already initialized")]
39    AlreadyInitialized,
40
41    #[error("WebSocket is already connected")]
42    AlreadyConnected,
43
44    #[error("WebSocket is not connected")]
45    NotConnected,
46
47    #[error("Unable to connect to {0}")]
48    Connect(String),
49
50    #[error("Handshake negotiation failure (internal)")]
51    NegotiationFailure,
52
53    #[error("Dispatch channel ack error")]
54    DispatchChannelAck,
55
56    #[error("Channel send error")]
57    ChannelSend,
58
59    #[error("Dispatch channel try_send error")]
60    DispatchChannelTrySend,
61
62    #[error("Dispatcher signal error")]
63    DispatcherSignal,
64
65    #[error("Receive channel error")]
66    ReceiveChannel,
67
68    #[error("Connect channel error")]
69    ConnectChannel,
70
71    #[error(transparent)]
72    Callback(#[from] workflow_wasm::callback::CallbackError),
73
74    #[error(transparent)]
75    Task(#[from] workflow_task::TaskError),
76
77    #[cfg(not(target_arch = "wasm32"))]
78    #[error("WebSocket error: {0}")]
79    Tungstenite(#[from] tokio_tungstenite::tungstenite::Error),
80
81    #[error("Unable to send ctl to receiver")]
82    ReceiverCtlSend(SendError<super::message::Message>),
83
84    #[error(transparent)]
85    WorkflowWasm(#[from] workflow_wasm::error::Error),
86
87    #[error("Connection timeout")]
88    ConnectionTimeout,
89
90    #[error("Invalid connect strategy argument: {0}")]
91    InvalidConnectStrategyArg(String),
92
93    #[error("Invalid connect strategy")]
94    InvalidConnectStrategy,
95}
96
97impl Error {
98    pub fn custom<T: std::fmt::Display>(message: T) -> Self {
99        Error::Custom(message.to_string())
100    }
101}
102
103impl From<String> for Error {
104    fn from(error: String) -> Error {
105        Error::Custom(error)
106    }
107}
108
109impl From<&str> for Error {
110    fn from(error: &str) -> Error {
111        Error::Custom(error.to_string())
112    }
113}
114
115impl From<Error> for JsValue {
116    fn from(error: Error) -> JsValue {
117        JsValue::from_str(&error.to_string())
118        // match error {
119        //     Error::JsValue(sendable) => sendable.into(),
120        //     _ => JsValue::from_str(&error.to_string()),
121        // }
122    }
123}
124
125impl From<JsValue> for Error {
126    fn from(error: JsValue) -> Error {
127        Error::JsValue(Sendable(Printable::new(error)))
128    }
129}
130
131impl<T> From<PoisonError<T>> for Error {
132    fn from(_: PoisonError<T>) -> Error {
133        Error::PoisonError
134    }
135}
136
137impl<T> From<SendError<T>> for Error {
138    fn from(_error: SendError<T>) -> Error {
139        Error::ChannelSend
140    }
141}
142
143impl<T> From<TrySendError<T>> for Error {
144    fn from(_error: TrySendError<T>) -> Error {
145        Error::ChannelSend
146    }
147}
148
149impl From<RecvError> for Error {
150    fn from(_: RecvError) -> Error {
151        Error::ReceiveChannel
152    }
153}