workflow_websocket/client/
error.rs1use std::sync::PoisonError;
2use thiserror::Error;
3use wasm_bindgen::JsValue;
5use workflow_core::channel::*;
6use workflow_core::sendable::*;
7use workflow_wasm::printable::*;
8
9#[derive(Error, Debug)]
11pub enum Error {
12 #[error("{0}")]
14 Custom(String),
15
16 #[error("{0}")]
18 JsValue(Sendable<Printable>),
19
20 #[error("PoisonError")]
22 PoisonError,
23
24 #[error("Missing WebSocket URL (must be supplied in constructor or the connect() method)")]
26 MissingUrl,
27
28 #[error("WebSocket URL must start with ws:// or wss:// - supplied argument is:`{0}`")]
30 AddressSchema(String),
31
32 #[error("Invalid message type")]
34 InvalidMessageType,
35
36 #[error("InvalidState")]
38 InvalidState(u16),
39
40 #[error("DataEncoding")]
42 DataEncoding,
43
44 #[error("DataType")]
46 DataType,
47
48 #[error("WebSocket connection already initialized")]
50 AlreadyInitialized,
51
52 #[error("WebSocket is already connected")]
54 AlreadyConnected,
55
56 #[error("WebSocket is not connected")]
58 NotConnected,
59
60 #[error("Unable to connect to {0}")]
62 Connect(String),
63
64 #[error("Handshake negotiation failure (internal)")]
66 NegotiationFailure,
67
68 #[error("Dispatch channel ack error")]
70 DispatchChannelAck,
71
72 #[error("Channel send error")]
74 ChannelSend,
75
76 #[error("Dispatch channel try_send error")]
78 DispatchChannelTrySend,
79
80 #[error("Dispatcher signal error")]
82 DispatcherSignal,
83
84 #[error("Receive channel error")]
86 ReceiveChannel,
87
88 #[error("Connect channel error")]
90 ConnectChannel,
91
92 #[error(transparent)]
94 Callback(#[from] workflow_wasm::callback::CallbackError),
95
96 #[error(transparent)]
98 Task(#[from] workflow_task::TaskError),
99
100 #[cfg(not(target_arch = "wasm32"))]
102 #[error("WebSocket error: {0}")]
103 Tungstenite(Box<tokio_tungstenite::tungstenite::Error>),
104
105 #[error("Unable to send ctl to receiver")]
107 ReceiverCtlSend(SendError<super::message::Message>),
108
109 #[error(transparent)]
111 WorkflowWasm(#[from] workflow_wasm::error::Error),
112
113 #[error("Connection timeout")]
115 ConnectionTimeout,
116
117 #[error("Invalid connect strategy argument: {0}")]
119 InvalidConnectStrategyArg(String),
120
121 #[error("Invalid connect strategy")]
123 InvalidConnectStrategy,
124}
125
126impl Error {
127 pub fn custom<T: std::fmt::Display>(message: T) -> Self {
129 Error::Custom(message.to_string())
130 }
131}
132
133#[cfg(not(target_arch = "wasm32"))]
134impl From<tokio_tungstenite::tungstenite::Error> for Error {
135 fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
136 Error::Tungstenite(Box::new(error))
137 }
138}
139
140impl From<String> for Error {
141 fn from(error: String) -> Error {
142 Error::Custom(error)
143 }
144}
145
146impl From<&str> for Error {
147 fn from(error: &str) -> Error {
148 Error::Custom(error.to_string())
149 }
150}
151
152impl From<Error> for JsValue {
153 fn from(error: Error) -> JsValue {
154 JsValue::from_str(&error.to_string())
155 }
160}
161
162impl From<JsValue> for Error {
163 fn from(error: JsValue) -> Error {
164 Error::JsValue(Sendable(Printable::new(error)))
165 }
166}
167
168impl<T> From<PoisonError<T>> for Error {
169 fn from(_: PoisonError<T>) -> Error {
170 Error::PoisonError
171 }
172}
173
174impl<T> From<SendError<T>> for Error {
175 fn from(_error: SendError<T>) -> Error {
176 Error::ChannelSend
177 }
178}
179
180impl<T> From<TrySendError<T>> for Error {
181 fn from(_error: TrySendError<T>) -> Error {
182 Error::ChannelSend
183 }
184}
185
186impl From<RecvError> for Error {
187 fn from(_: RecvError) -> Error {
188 Error::ReceiveChannel
189 }
190}