Skip to main content

workflow_rpc/
error.rs

1//!
2//! Common [`enum@Error`] definitions used by both [`super::client`] and [`super::server`] modules.
3//!
4
5use borsh::{BorshDeserialize, BorshSerialize};
6use serde::*;
7use std::sync::PoisonError;
8use thiserror::Error;
9use workflow_core::channel::{RecvError, SendError, TrySendError};
10
11/// Errors shared by the wRPC client and server message-handling layers.
12#[derive(Error, Debug)]
13pub enum Error {
14    /// Received message is smaller than the minimum header size
15    #[error("Invalid header size")]
16    HeaderSize,
17
18    /// An underlying I/O error.
19    #[error(transparent)]
20    Io(#[from] std::io::Error),
21
22    /// An error originating from the underlying async task subsystem.
23    #[error(transparent)]
24    Task(#[from] workflow_task::TaskError),
25
26    /// The requested message encoding is unknown or unsupported.
27    #[error("invalid encoding {0}")]
28    Encoding(String),
29}
30
31///
32/// [`ServerError`] enum is used by both Server and Client and
33/// represents errors returned by server-side handlers. This enum
34/// is also serialized and transported to the client when using
35/// the `Borsh` protocol (as such, this mostly contains pure enum
36/// values).
37///
38#[derive(
39    Error, Debug, Clone, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
40)]
41pub enum ServerError {
42    /// The connection has been closed.
43    #[error("connection is closed")]
44    Close,
45    /// The RPC call did not complete within the allotted time.
46    #[error("RPC call timed out")]
47    Timeout,
48    /// No data was available where data was expected.
49    #[error("no data")]
50    NoData,
51    /// No handler was registered for the requested RPC method.
52    #[error("RPC method not found")]
53    NotFound,
54    /// A lock was poisoned by a panic in another thread.
55    #[error("resource lock error")]
56    PoisonError,
57    /// The request was expected to use the Borsh protocol but did not.
58    #[error("not a borsh request")]
59    NonBorshRequest,
60    /// The request was expected to use the serde/JSON protocol but did not.
61    #[error("not a serde request")]
62    NonSerdeRequest,
63    /// The request could not be serialized.
64    #[error("request serialization error")]
65    ReqSerialize,
66    /// The request could not be deserialized.
67    #[error("request deserialization error")]
68    ReqDeserialize,
69    /// The response could not be serialized.
70    #[error("response serialization error")]
71    RespSerialize,
72    /// A notification payload could not be deserialized.
73    #[error("request deserialization error")]
74    NotificationDeserialize(String),
75    /// A response payload could not be deserialized.
76    #[error("response deserialization error")]
77    RespDeserialize(String),
78    /// Opaque binary error payload.
79    #[error("data")]
80    Data(Vec<u8>),
81    /// Free-form textual error message.
82    #[error("{0}")]
83    Text(String),
84    /// Underlying WebSocket error
85    #[error("WebSocket -> {0}")]
86    WebSocketError(String),
87    /// Failure receiving from an internal receiver channel.
88    #[error("Receiver channel")]
89    ReceiveChannelRx,
90    /// Failure sending to an internal receiver channel.
91    #[error("Receiver channel send")]
92    ReceiveChannelTx,
93}
94
95impl From<std::io::Error> for ServerError {
96    fn from(_err: std::io::Error) -> Self {
97        ServerError::RespSerialize
98    }
99}
100
101impl<T> From<PoisonError<T>> for ServerError {
102    fn from(_error: PoisonError<T>) -> ServerError {
103        ServerError::PoisonError
104    }
105}
106
107impl From<String> for ServerError {
108    fn from(error: String) -> Self {
109        ServerError::Text(error)
110    }
111}
112
113impl From<&str> for ServerError {
114    fn from(error: &str) -> Self {
115        ServerError::Text(error.to_string())
116    }
117}
118
119// impl From<serde_json::Error> for ServerError
120
121// impl de::Error for Error {
122//     fn custom<T: Display>(msg: T) -> Error {
123//         Error::SerdeDeserialize(msg.to_string())
124//     }
125// }
126
127// impl ser::Error for Error {
128//     fn custom<T: Display>(msg: T) -> Error {
129//         Error::SerdeSerialize(msg.to_string())
130//     }
131// }
132
133impl From<workflow_websocket::client::Error> for ServerError {
134    fn from(error: workflow_websocket::client::Error) -> Self {
135        ServerError::WebSocketError(error.to_string())
136    }
137}
138
139impl From<RecvError> for ServerError {
140    fn from(_: RecvError) -> ServerError {
141        ServerError::ReceiveChannelRx
142    }
143}
144
145impl<T> From<SendError<T>> for ServerError {
146    fn from(_error: SendError<T>) -> ServerError {
147        ServerError::ReceiveChannelTx
148    }
149}
150
151impl<T> From<TrySendError<T>> for ServerError {
152    fn from(_error: TrySendError<T>) -> ServerError {
153        ServerError::ReceiveChannelTx
154    }
155}