workflow_rpc/client/
error.rs

1//!
2//! Client [`enum@Error`] enum declaration
3//!
4
5use crate::error::ServerError;
6use crate::messages::serde_json::JsonServerError;
7use serde::*;
8use std::fmt::Display;
9use thiserror::Error;
10use wasm_bindgen::JsValue;
11use workflow_core::channel::{RecvError, SendError, TrySendError};
12pub use workflow_websocket::client::error::Error as WebSocketError;
13
14#[derive(Error, Debug)]
15pub enum Error {
16    #[error("Invalid event '{0}'")]
17    InvalidEvent(String),
18
19    #[error("Invalid URL {0}")]
20    InvalidUrl(String),
21
22    #[error(transparent)]
23    RpcError(#[from] crate::error::Error),
24
25    #[error("response handler for id {0} not found")]
26    ResponseHandler(String),
27
28    #[error("WebSocket disconnected")]
29    Disconnect,
30
31    #[error("Missing method in notification message")]
32    NotificationMethod,
33
34    #[error("invalid WebSocket message type for protocol")]
35    WebSocketMessageType,
36
37    #[error("RPC client is missing notification handler")]
38    MissingNotificationHandler,
39
40    /// Underlying WebSocket error
41    #[error("WebSocket -> {0}")]
42    WebSocketError(#[from] WebSocketError),
43    /// RPC call timeout
44    #[error("RPC request timeout")]
45    Timeout,
46    /// Unable to send shutdown message to receiver
47    #[error("Receiver ctl failure")]
48    ReceiverCtl,
49    /// RPC call succeeded but no data was received in success response
50    #[error("RPC has no data in success response")]
51    NoDataInSuccessResponse,
52    #[error("RPC has no data in notification")]
53    NoDataInNotificationMessage,
54    /// RPC call failed but no data was received in error response
55    #[error("RPC has no data in the error response")]
56    NoDataInErrorResponse,
57    /// Unable to deserialize response data
58    #[error("RPC error deserializing server message data")]
59    ErrorDeserializingServerMessageData(crate::error::Error),
60    /// Unable to deserialize response data
61    #[error("RPC error deserializing response data")]
62    ErrorDeserializingResponseData,
63    /// Response produced an unknown status code
64    #[error("RPC status code {0}")]
65    StatusCode(u32),
66    /// RPC call executed successfully but produced an error response
67    #[error("RPC response error {0:?}")]
68    RpcCall(ServerError),
69    /// Unable to serialize borsh data    
70    #[error("RPC borsh serialization error")]
71    BorshSerialize,
72    /// Unable to deserialize borsh data
73    #[error("RPC borsh deserialization error: {0}")]
74    BorshDeserialize(String),
75    /// Unable to serialize serde data    
76    #[error("RPC serde serialization error: {0}")]
77    SerdeSerialize(String), //#[from] dyn serde::de::Error),
78    /// Unable to deserialize serde data
79    #[error("RPC serde deserialization error: {0}")]
80    SerdeDeserialize(String),
81    /// RPC call succeeded, but error occurred deserializing borsh response
82    #[error("RPC borsh error deserializing response: {0}")]
83    BorshResponseDeserialize(String),
84
85    #[error("RPC: channel receive error")]
86    ChannelRecvError,
87
88    #[error("RPC: channel send error")]
89    ChannelSendError,
90
91    #[error("Utf8 error: {0}")]
92    Utf8Error(#[from] std::str::Utf8Error),
93
94    #[error("SerdeJSON error: {0}")]
95    SerdeJSON(#[from] serde_json::Error),
96
97    #[error(transparent)]
98    Task(#[from] workflow_task::TaskError),
99
100    #[error("{0}")]
101    ServerError(ServerError),
102
103    #[error("{0}")]
104    JsonServerError(JsonServerError),
105    // #[error("{0}")]
106    // RegexError(#[from] regex::Error),
107}
108
109impl From<ServerError> for Error {
110    fn from(err: ServerError) -> Self {
111        Error::ServerError(err)
112    }
113}
114
115/// Transform Error into JsValue containing the error message
116impl From<Error> for JsValue {
117    fn from(err: Error) -> JsValue {
118        JsValue::from(err.to_string())
119    }
120}
121
122impl From<RecvError> for Error {
123    fn from(_: RecvError) -> Self {
124        Error::ChannelRecvError
125    }
126}
127
128impl<T> From<SendError<T>> for Error {
129    fn from(_: SendError<T>) -> Self {
130        Error::ChannelSendError
131    }
132}
133
134impl<T> From<TrySendError<T>> for Error {
135    fn from(_: TrySendError<T>) -> Self {
136        Error::ChannelSendError
137    }
138}
139
140impl de::Error for Error {
141    fn custom<T: Display>(msg: T) -> Error {
142        Error::SerdeDeserialize(msg.to_string())
143    }
144}
145
146impl ser::Error for Error {
147    fn custom<T: Display>(msg: T) -> Error {
148        Error::SerdeSerialize(msg.to_string())
149    }
150}
151
152impl From<JsonServerError> for Error {
153    fn from(err: JsonServerError) -> Self {
154        Error::JsonServerError(err)
155    }
156}