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#[derive(Error, Debug)]
12pub enum Error {
13    /// Received message is smaller than the minimum header size
14    #[error("Invalid header size")]
15    HeaderSize,
16
17    #[error(transparent)]
18    Io(#[from] std::io::Error),
19
20    #[error(transparent)]
21    Task(#[from] workflow_task::TaskError),
22
23    #[error("invalid encoding {0}")]
24    Encoding(String),
25}
26
27///
28/// [`ServerError`] enum is used by both Server and Client and
29/// represents errors returned by server-side handlers. This enum
30/// is also serialized and transported to the client when using
31/// the `Borsh` protocol (as such, this mostly contains pure enum
32/// values).
33///
34#[derive(
35    Error, Debug, Clone, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
36)]
37pub enum ServerError {
38    #[error("connection is closed")]
39    Close,
40    #[error("RPC call timed out")]
41    Timeout,
42    #[error("no data")]
43    NoData,
44    #[error("RPC method not found")]
45    NotFound,
46    #[error("resource lock error")]
47    PoisonError,
48    #[error("not a borsh request")]
49    NonBorshRequest,
50    #[error("not a serde request")]
51    NonSerdeRequest,
52    #[error("request serialization error")]
53    ReqSerialize,
54    #[error("request deserialization error")]
55    ReqDeserialize,
56    #[error("response serialization error")]
57    RespSerialize,
58    #[error("request deserialization error")]
59    NotificationDeserialize(String),
60    #[error("response deserialization error")]
61    RespDeserialize(String),
62    #[error("data")]
63    Data(Vec<u8>),
64    #[error("{0}")]
65    Text(String),
66    /// Underlying WebSocket error
67    #[error("WebSocket -> {0}")]
68    WebSocketError(String),
69    #[error("Receiver channel")]
70    ReceiveChannelRx,
71    #[error("Receiver channel send")]
72    ReceiveChannelTx,
73}
74
75impl From<std::io::Error> for ServerError {
76    fn from(_err: std::io::Error) -> Self {
77        ServerError::RespSerialize
78    }
79}
80
81impl<T> From<PoisonError<T>> for ServerError {
82    fn from(_error: PoisonError<T>) -> ServerError {
83        ServerError::PoisonError
84    }
85}
86
87impl From<String> for ServerError {
88    fn from(error: String) -> Self {
89        ServerError::Text(error)
90    }
91}
92
93impl From<&str> for ServerError {
94    fn from(error: &str) -> Self {
95        ServerError::Text(error.to_string())
96    }
97}
98
99// impl From<serde_json::Error> for ServerError
100
101// impl de::Error for Error {
102//     fn custom<T: Display>(msg: T) -> Error {
103//         Error::SerdeDeserialize(msg.to_string())
104//     }
105// }
106
107// impl ser::Error for Error {
108//     fn custom<T: Display>(msg: T) -> Error {
109//         Error::SerdeSerialize(msg.to_string())
110//     }
111// }
112
113impl From<workflow_websocket::client::Error> for ServerError {
114    fn from(error: workflow_websocket::client::Error) -> Self {
115        ServerError::WebSocketError(error.to_string())
116    }
117}
118
119impl From<RecvError> for ServerError {
120    fn from(_: RecvError) -> ServerError {
121        ServerError::ReceiveChannelRx
122    }
123}
124
125impl<T> From<SendError<T>> for ServerError {
126    fn from(_error: SendError<T>) -> ServerError {
127        ServerError::ReceiveChannelTx
128    }
129}
130
131impl<T> From<TrySendError<T>> for ServerError {
132    fn from(_error: TrySendError<T>) -> ServerError {
133        ServerError::ReceiveChannelTx
134    }
135}