Skip to main content

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/// Errors produced by the WebSocket client.
10#[derive(Error, Debug)]
11pub enum Error {
12    /// A custom, free-form error message.
13    #[error("{0}")]
14    Custom(String),
15
16    /// An error originating from the JavaScript/WASM boundary.
17    #[error("{0}")]
18    JsValue(Sendable<Printable>),
19
20    /// A mutex was poisoned due to a panic in another thread.
21    #[error("PoisonError")]
22    PoisonError,
23
24    /// No URL was supplied via the constructor, `connect()`, or a resolver.
25    #[error("Missing WebSocket URL (must be supplied in constructor or the connect() method)")]
26    MissingUrl,
27
28    /// The supplied URL does not use the `ws://` or `wss://` scheme.
29    #[error("WebSocket URL must start with ws:// or wss:// - supplied argument is:`{0}`")]
30    AddressSchema(String),
31
32    /// A received message was of an unsupported or unexpected type.
33    #[error("Invalid message type")]
34    InvalidMessageType,
35
36    /// The socket was in an invalid ready state (value carried) for the operation.
37    #[error("InvalidState")]
38    InvalidState(u16),
39
40    /// Message payload could not be encoded.
41    #[error("DataEncoding")]
42    DataEncoding,
43
44    /// The message data was of an unexpected type.
45    #[error("DataType")]
46    DataType,
47
48    /// The connection has already been initialized.
49    #[error("WebSocket connection already initialized")]
50    AlreadyInitialized,
51
52    /// The WebSocket is already connected.
53    #[error("WebSocket is already connected")]
54    AlreadyConnected,
55
56    /// The WebSocket is not currently connected.
57    #[error("WebSocket is not connected")]
58    NotConnected,
59
60    /// Connection to the given URL could not be established.
61    #[error("Unable to connect to {0}")]
62    Connect(String),
63
64    /// The custom handshake negotiation failed.
65    #[error("Handshake negotiation failure (internal)")]
66    NegotiationFailure,
67
68    /// Failed to receive an acknowledgement on the dispatch channel.
69    #[error("Dispatch channel ack error")]
70    DispatchChannelAck,
71
72    /// A channel send operation failed.
73    #[error("Channel send error")]
74    ChannelSend,
75
76    /// A non-blocking dispatch channel `try_send` failed.
77    #[error("Dispatch channel try_send error")]
78    DispatchChannelTrySend,
79
80    /// Failed to signal the dispatcher task.
81    #[error("Dispatcher signal error")]
82    DispatcherSignal,
83
84    /// The receive channel failed.
85    #[error("Receive channel error")]
86    ReceiveChannel,
87
88    /// The connect-notification channel failed.
89    #[error("Connect channel error")]
90    ConnectChannel,
91
92    /// An error originating from a WASM callback.
93    #[error(transparent)]
94    Callback(#[from] workflow_wasm::callback::CallbackError),
95
96    /// An error originating from the task subsystem.
97    #[error(transparent)]
98    Task(#[from] workflow_task::TaskError),
99
100    /// An error from the native `tungstenite` WebSocket implementation.
101    #[cfg(not(target_arch = "wasm32"))]
102    #[error("WebSocket error: {0}")]
103    Tungstenite(Box<tokio_tungstenite::tungstenite::Error>),
104
105    /// Failed to relay a control message to the receiver.
106    #[error("Unable to send ctl to receiver")]
107    ReceiverCtlSend(SendError<super::message::Message>),
108
109    /// An error originating from the `workflow-wasm` crate.
110    #[error(transparent)]
111    WorkflowWasm(#[from] workflow_wasm::error::Error),
112
113    /// The connection attempt timed out.
114    #[error("Connection timeout")]
115    ConnectionTimeout,
116
117    /// The supplied connect-strategy argument was invalid.
118    #[error("Invalid connect strategy argument: {0}")]
119    InvalidConnectStrategyArg(String),
120
121    /// The configured connect strategy was invalid.
122    #[error("Invalid connect strategy")]
123    InvalidConnectStrategy,
124}
125
126impl Error {
127    /// Construct a [`Error::Custom`] error from any displayable value.
128    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        // match error {
156        //     Error::JsValue(sendable) => sendable.into(),
157        //     _ => JsValue::from_str(&error.to_string()),
158        // }
159    }
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}