wick_rpc/
error.rs

1use thiserror::Error;
2
3/// The RPC Error type.
4#[derive(Error, Debug)]
5#[non_exhaustive]
6pub enum RpcError {
7  /// Error during the parsing of an IP address and port.
8  #[error(transparent)]
9  AddrParseError(#[from] std::net::AddrParseError),
10
11  /// Error parsing a UUID.
12  #[error("Could not parse UUID '{0}': {1}")]
13  UuidParseError(String, uuid::Error),
14
15  /// Upstream error from Tonic.
16  #[error(transparent)]
17  TransportError(#[from] tonic::transport::Error),
18
19  /// Internal Error.
20  #[error("Internal Error: {0}")]
21  InternalError(String),
22
23  /// No inherent data found in RPC message.
24  #[error("No inherent data found in RPC message")]
25  NoInherentData,
26
27  /// Conversion error between types.
28  #[error("Could not convert type to or from gRPC to wick.")]
29  TypeConversion,
30
31  /// Invalid ComponentKind.
32  #[error("Invalid component kind {0}")]
33  InvalidComponentKind(i32),
34
35  /// Error used by components.
36  #[error("{0}")]
37  Component(String),
38
39  /// Component did not include a supported feature list.
40  #[error("Component did not include a supported feature list.")]
41  MissingFeatures,
42
43  /// Error generated by a component's operations.
44  #[error("{0}")]
45  Operation(String),
46
47  /// Error sending output to channel.
48  #[error("Error sending output to channel")]
49  SendError,
50
51  /// General Error.
52  #[error("General error : {0}")]
53  General(String),
54
55  /// Deserialization Failed.
56  #[error("Deserialization Failed : {0}")]
57  Deserialization(String),
58
59  /// Error caused by an internal inconsistency.
60  #[error("Internal Error : {0}")]
61  Internal(&'static str),
62
63  /// Configuration for invocation was empty.
64  #[error("Configuration for invocation was empty.")]
65  ConfigEmpty,
66
67  /// Configuration for invocation was empty.
68  #[error("State for invocation was missing.")]
69  StateMissing,
70
71  /// Invalid Type Signature.
72  #[error("Invalid signature")]
73  InvalidSignature,
74}
75
76impl RpcError {
77  /// Constructor for a [Box<RpcError::General>]
78  pub fn boxed<T: std::fmt::Display>(msg: T) -> Box<Self> {
79    Box::new(RpcError::General(msg.to_string()))
80  }
81}
82
83impl From<tokio::task::JoinError> for RpcError {
84  fn from(e: tokio::task::JoinError) -> Self {
85    RpcError::InternalError(format!("Tokio Error: {}", e))
86  }
87}
88
89impl From<std::io::Error> for RpcError {
90  fn from(e: std::io::Error) -> Self {
91    RpcError::InternalError(format!("IO Error: {}", e))
92  }
93}
94
95impl From<Box<dyn std::error::Error + Send + Sync>> for RpcError {
96  fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
97    RpcError::Component(e.to_string())
98  }
99}
100
101impl From<&str> for RpcError {
102  fn from(e: &str) -> Self {
103    RpcError::General(e.to_owned())
104  }
105}
106
107impl From<String> for RpcError {
108  fn from(e: String) -> Self {
109    RpcError::General(e)
110  }
111}
112
113/// The error type that [crate::RpcClient] methods produce.
114#[derive(thiserror::Error, Debug)]
115#[non_exhaustive]
116pub enum RpcClientError {
117  /// An error originating from a List RPC call.
118  #[error("RPC List call failed: {0}")]
119  ListCallFailed(tonic::Status),
120
121  /// An error originating from an Invocation RPC call.
122  #[error("RPC Invocation failed: {0}")]
123  InvocationFailed(tonic::Status),
124
125  /// An error originating from a Stats RPC call.
126  #[error("RPC Stats call failed: {0}")]
127  StatsCallFailed(tonic::Status),
128
129  /// Invalid response from RPC call.
130  #[error("RPC response invalid: {0}")]
131  ResponseInvalid(String),
132
133  /// Error converting to or from RPC data types.
134  #[error(transparent)]
135  ConversionFailed(RpcError),
136
137  /// Conversion error between types.
138  #[error("Could not convert type to or from gRPC to wick: {0}")]
139  TypeConversion(String),
140
141  /// General IO error
142  #[error("I/O error: {0}")]
143  IO(std::io::Error),
144
145  /// Error with Tls configuration
146  #[error("Tls error: {0}")]
147  TlsError(tonic::transport::Error),
148
149  /// Error connecting to service
150  #[error("{0}")]
151  ConnectionError(String),
152
153  /// Unspecified error connecting to service
154  #[error("unspecified connection error")]
155  UnspecifiedConnectionError,
156
157  /// Connection failed
158  #[error("Connection failed: {0}")]
159  ConnectionFailed(String),
160
161  /// General error
162  #[error("{0}")]
163  Other(String),
164}
165
166impl From<std::io::Error> for RpcClientError {
167  fn from(e: std::io::Error) -> Self {
168    RpcClientError::IO(e)
169  }
170}
171
172#[cfg(test)]
173mod test {
174
175  use super::*;
176  const fn sync_send<T>()
177  where
178    T: Sync + Send,
179  {
180  }
181
182  #[test]
183  const fn test_sync_send() {
184    sync_send::<RpcError>();
185    sync_send::<RpcClientError>();
186  }
187}