1use super::errors::HandlerError;
4use serde::{de::DeserializeOwned, Deserialize, Serialize};
5use thiserror::Error;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct RequestEnvelope {
10 pub handler_type: String,
11 pub handler_id: String,
12 pub message_type: String,
13 pub payload: Vec<u8>,
14}
15
16impl RequestEnvelope {
17 pub fn new(
18 handler_type: String,
19 handler_id: String,
20 message_type: String,
21 payload: Vec<u8>,
22 ) -> RequestEnvelope {
23 RequestEnvelope {
24 handler_type,
25 handler_id,
26 message_type,
27 payload,
28 }
29 }
30}
31
32#[derive(Debug, Serialize, Deserialize)]
47pub struct ResponseEnvelope {
48 pub body: Result<Vec<u8>, ResponseError>,
49}
50
51impl ResponseEnvelope {
52 pub fn new(body: Vec<u8>) -> ResponseEnvelope {
54 ResponseEnvelope { body: Ok(body) }
55 }
56
57 pub fn err(error: ResponseError) -> ResponseEnvelope {
59 ResponseEnvelope { body: Err(error) }
60 }
61}
62
63impl From<HandlerError> for ResponseEnvelope {
68 fn from(error: HandlerError) -> Self {
69 let response_err = ResponseError::from(error);
70 ResponseEnvelope {
71 body: Err(response_err),
72 }
73 }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Error, Serialize, Deserialize)]
78pub enum ResponseError {
79 #[error("ServiceObject is in another server")]
80 Redirect(String),
81
82 #[error("ServiceObject had to be deallocated")]
83 DeallocateServiceObject,
84
85 #[error("ServiceObject could not be allocated")]
86 Allocate,
87
88 #[error("ServiceObject not supported")]
89 NotSupported(String),
90
91 #[error("unknown execution error")]
92 Unknown(String),
93
94 #[error("handler error")]
95 HandlerError(String),
96
97 #[error("error deserializing response")]
98 DeseralizationError(String),
99
100 #[error("error serializing message")]
101 SeralizationError(String),
102
103 #[error("Error caused by the application, serialized in bincode")]
104 ApplicationError(Vec<u8>),
105}
106
107impl From<HandlerError> for ResponseError {
112 fn from(error: HandlerError) -> Self {
113 match error {
114 HandlerError::ApplicationError(v) => ResponseError::ApplicationError(v),
115 inner_err => ResponseError::Unknown(inner_err.to_string()),
116 }
117 }
118}
119
120#[derive(Error, Debug, PartialEq, Eq)]
123pub enum ClientError {
124 #[error("no servers available")]
125 NoServersAvailable,
126
127 #[error("the requested server is not available")]
128 ServerNotAvailable(String),
129
130 #[error("client was disconnected from the server (no more items on the TCP stream)")]
131 Disconnect,
132
133 #[error("rendenvouz is not available")]
134 RendevouzUnavailable,
135
136 #[error("connectivity error")]
137 Connectivity,
138
139 #[error("unknown client error")]
140 Unknown(String),
141
142 #[error("unknown PlacementLock error")]
143 PlacementLock,
144
145 #[error("error deserializing response")]
146 DeseralizationError(String),
147
148 #[error("error serializing message")]
149 SeralizationError(String),
150
151 #[error("std::io::Error")]
152 IoError(String),
153}
154
155impl From<::std::io::Error> for ClientError {
156 fn from(error: ::std::io::Error) -> Self {
157 ClientError::IoError(error.to_string())
158 }
159}
160
161#[derive(Debug, Clone, PartialEq, Eq, Error, Serialize, Deserialize)]
164pub enum NoopError {}
165
166#[derive(Error, Debug, PartialEq, Eq)]
168pub enum RequestError<E: std::error::Error> {
169 #[error("error in the service response")]
170 ResponseError(ResponseError),
171
172 #[error("client error")]
173 ClientError(ClientError),
174
175 #[error("application error")]
176 ApplicationError(E),
177}
178
179impl<E: std::error::Error> From<::std::io::Error> for RequestError<E> {
180 fn from(error: ::std::io::Error) -> Self {
181 Into::<ClientError>::into(error).into()
182 }
183}
184
185impl<E: std::error::Error> From<ClientError> for RequestError<E> {
196 fn from(err: ClientError) -> Self {
197 RequestError::ClientError(err)
198 }
199}
200
201impl<E: std::error::Error + DeserializeOwned> From<ResponseError> for RequestError<E> {
202 fn from(err: ResponseError) -> Self {
203 match err {
204 ResponseError::ApplicationError(ser_error) => {
205 let des_result: Result<E, _> = bincode::deserialize(&ser_error);
207 match des_result {
208 Ok(err) => Self::ApplicationError(err),
209 Err(bincode_err) => {
210 let error_message =
211 format!("Application error deserialization issue: {}", bincode_err);
212 let des_error = ResponseError::DeseralizationError(error_message);
213 Self::ResponseError(des_error)
214 }
215 }
216 }
217 rest => RequestError::ResponseError(rest),
218 }
219 }
220}
221
222pub mod pubsub {
223 use super::*;
224
225 #[derive(Debug, Serialize, Deserialize)]
228 pub struct SubscriptionRequest {
229 pub handler_type: String,
230 pub handler_id: String,
231 }
232
233 #[derive(Debug, Clone, Serialize, Deserialize)]
235 pub struct SubscriptionResponse {
236 pub body: Result<Vec<u8>, ResponseError>,
237 }
238
239 impl SubscriptionResponse {
240 pub fn new(body: Vec<u8>) -> Self {
242 SubscriptionResponse { body: Ok(body) }
243 }
244
245 pub fn err(error: ResponseError) -> Self {
247 SubscriptionResponse { body: Err(error) }
248 }
249 }
250}