Skip to main content

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/// Errors produced by the wRPC client.
15#[derive(Error, Debug)]
16pub enum Error {
17    /// A control event string could not be parsed into a known [`super::Ctl`] value.
18    #[error("Invalid event '{0}'")]
19    InvalidEvent(String),
20
21    /// The supplied endpoint URL is malformed or unsupported.
22    #[error("Invalid URL {0}")]
23    InvalidUrl(String),
24
25    /// An error originating from the shared RPC error type.
26    #[error(transparent)]
27    RpcError(#[from] crate::error::Error),
28
29    /// No pending response handler was registered for the given message id.
30    #[error("response handler for id {0} not found")]
31    ResponseHandler(String),
32
33    /// The WebSocket connection was disconnected.
34    #[error("WebSocket disconnected")]
35    Disconnect,
36
37    /// A notification message did not include a method identifier.
38    #[error("Missing method in notification message")]
39    NotificationMethod,
40
41    /// The received WebSocket message type is incompatible with the active protocol.
42    #[error("invalid WebSocket message type for protocol")]
43    WebSocketMessageType,
44
45    /// A notification was received but no notification handler is configured.
46    #[error("RPC client is missing notification handler")]
47    MissingNotificationHandler,
48
49    /// Underlying WebSocket error
50    #[error("WebSocket -> {0}")]
51    WebSocketError(#[from] WebSocketError),
52    /// RPC call timeout
53    #[error("RPC request timeout")]
54    Timeout,
55    /// Unable to send shutdown message to receiver
56    #[error("Receiver ctl failure")]
57    ReceiverCtl,
58    /// RPC call succeeded but no data was received in success response
59    #[error("RPC has no data in success response")]
60    NoDataInSuccessResponse,
61    /// A notification message arrived without any payload data.
62    #[error("RPC has no data in notification")]
63    NoDataInNotificationMessage,
64    /// RPC call failed but no data was received in error response
65    #[error("RPC has no data in the error response")]
66    NoDataInErrorResponse,
67    /// Unable to deserialize response data
68    #[error("RPC error deserializing server message data")]
69    ErrorDeserializingServerMessageData(crate::error::Error),
70    /// Unable to deserialize response data
71    #[error("RPC error deserializing response data")]
72    ErrorDeserializingResponseData,
73    /// Response produced an unknown status code
74    #[error("RPC status code {0}")]
75    StatusCode(u32),
76    /// RPC call executed successfully but produced an error response
77    #[error("RPC response error {0:?}")]
78    RpcCall(ServerError),
79    /// Unable to serialize borsh data    
80    #[error("RPC borsh serialization error")]
81    BorshSerialize,
82    /// Unable to deserialize borsh data
83    #[error("RPC borsh deserialization error: {0}")]
84    BorshDeserialize(String),
85    /// Unable to serialize serde data    
86    #[error("RPC serde serialization error: {0}")]
87    SerdeSerialize(String), //#[from] dyn serde::de::Error),
88    /// Unable to deserialize serde data
89    #[error("RPC serde deserialization error: {0}")]
90    SerdeDeserialize(String),
91    /// RPC call succeeded, but error occurred deserializing borsh response
92    #[error("RPC borsh error deserializing response: {0}")]
93    BorshResponseDeserialize(String),
94
95    /// Failure receiving from an internal channel.
96    #[error("RPC: channel receive error")]
97    ChannelRecvError,
98
99    /// Failure sending to an internal channel.
100    #[error("RPC: channel send error")]
101    ChannelSendError,
102
103    /// A byte sequence could not be interpreted as valid UTF-8.
104    #[error("Utf8 error: {0}")]
105    Utf8Error(#[from] std::str::Utf8Error),
106
107    /// An error from JSON serialization or deserialization.
108    #[error("SerdeJSON error: {0}")]
109    SerdeJSON(#[from] serde_json::Error),
110
111    /// An error originating from the underlying async task subsystem.
112    #[error(transparent)]
113    Task(#[from] workflow_task::TaskError),
114
115    /// A server-side error returned over the Borsh protocol.
116    #[error("{0}")]
117    ServerError(ServerError),
118
119    /// A server-side error returned over the JSON protocol.
120    #[error("{0}")]
121    JsonServerError(JsonServerError),
122    // #[error("{0}")]
123    // RegexError(#[from] regex::Error),
124}
125
126impl From<ServerError> for Error {
127    fn from(err: ServerError) -> Self {
128        Error::ServerError(err)
129    }
130}
131
132/// Transform Error into JsValue containing the error message
133impl From<Error> for JsValue {
134    fn from(err: Error) -> JsValue {
135        JsValue::from(err.to_string())
136    }
137}
138
139impl From<RecvError> for Error {
140    fn from(_: RecvError) -> Self {
141        Error::ChannelRecvError
142    }
143}
144
145impl<T> From<SendError<T>> for Error {
146    fn from(_: SendError<T>) -> Self {
147        Error::ChannelSendError
148    }
149}
150
151impl<T> From<TrySendError<T>> for Error {
152    fn from(_: TrySendError<T>) -> Self {
153        Error::ChannelSendError
154    }
155}
156
157impl de::Error for Error {
158    fn custom<T: Display>(msg: T) -> Error {
159        Error::SerdeDeserialize(msg.to_string())
160    }
161}
162
163impl ser::Error for Error {
164    fn custom<T: Display>(msg: T) -> Error {
165        Error::SerdeSerialize(msg.to_string())
166    }
167}
168
169impl From<JsonServerError> for Error {
170    fn from(err: JsonServerError) -> Self {
171        Error::JsonServerError(err)
172    }
173}