1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2018-2020 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Errors that can occur in a service and service processor
use std::error::Error;
use std::io::Error as IOError;

use protobuf::error::ProtobufError;

#[derive(Debug)]
pub struct ServiceSendError(pub Box<dyn Error + Send>);

impl Error for ServiceSendError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&*self.0)
    }
}

impl std::fmt::Display for ServiceSendError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "unable to send message: {}", self.0)
    }
}

#[derive(Debug)]
pub enum ServiceConnectionError {
    ConnectionError(Box<dyn Error + Send>),
    RejectedError(String),
    WrongResponse(String),
}

impl Error for ServiceConnectionError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ServiceConnectionError::ConnectionError(err) => Some(&**err),
            ServiceConnectionError::RejectedError(_) => None,
            ServiceConnectionError::WrongResponse(_) => None,
        }
    }
}

impl std::fmt::Display for ServiceConnectionError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            ServiceConnectionError::ConnectionError(ref err) => {
                write!(f, "unable to connect service: {}", err)
            }
            ServiceConnectionError::RejectedError(ref err) => {
                write!(f, "connection request was rejected: {}", err)
            }
            ServiceConnectionError::WrongResponse(ref err) => {
                write!(f, "wrong response type was returned: {}", err)
            }
        }
    }
}

#[derive(Debug)]
pub enum ServiceDisconnectionError {
    DisconnectionError(Box<dyn Error + Send>),
    RejectedError(String),
    WrongResponse(String),
}

impl Error for ServiceDisconnectionError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ServiceDisconnectionError::DisconnectionError(err) => Some(&**err),
            ServiceDisconnectionError::RejectedError(_) => None,
            ServiceDisconnectionError::WrongResponse(_) => None,
        }
    }
}

impl std::fmt::Display for ServiceDisconnectionError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            ServiceDisconnectionError::DisconnectionError(ref err) => {
                write!(f, "unable to disconnect service: {}", err)
            }
            ServiceDisconnectionError::RejectedError(ref err) => {
                write!(f, "disconnection request was rejected: {}", err)
            }
            ServiceDisconnectionError::WrongResponse(ref err) => {
                write!(f, "wrong response type was returned: {}", err)
            }
        }
    }
}

#[derive(Debug)]
pub enum ServiceStartError {
    AlreadyStarted,
    UnableToConnect(ServiceConnectionError),
    Internal(Box<dyn Error + Send>),
    PoisonedLock(String),
}

impl Error for ServiceStartError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ServiceStartError::AlreadyStarted => None,
            ServiceStartError::UnableToConnect(err) => Some(err),
            ServiceStartError::Internal(err) => Some(&**err),
            ServiceStartError::PoisonedLock(_) => None,
        }
    }
}

impl std::fmt::Display for ServiceStartError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ServiceStartError::AlreadyStarted => write!(f, "service already started"),
            ServiceStartError::UnableToConnect(err) => {
                write!(f, "unable to connect on start: {}", err)
            }
            ServiceStartError::Internal(err) => write!(f, "unable to start service: {}", err),
            ServiceStartError::PoisonedLock(msg) => write!(f, "a lock was poisoned: {}", msg),
        }
    }
}

impl From<ServiceConnectionError> for ServiceStartError {
    fn from(err: ServiceConnectionError) -> Self {
        ServiceStartError::UnableToConnect(err)
    }
}

#[derive(Debug)]
pub enum ServiceStopError {
    NotStarted,
    UnableToDisconnect(ServiceDisconnectionError),
    Internal(Box<dyn Error + Send>),
    PoisonedLock(String),
}

impl Error for ServiceStopError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ServiceStopError::NotStarted => None,
            ServiceStopError::UnableToDisconnect(err) => Some(err),
            ServiceStopError::Internal(err) => Some(&**err),
            ServiceStopError::PoisonedLock(_) => None,
        }
    }
}

impl std::fmt::Display for ServiceStopError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ServiceStopError::NotStarted => write!(f, "service not started"),
            ServiceStopError::UnableToDisconnect(err) => {
                write!(f, "unable to disconnect on stop: {}", err)
            }
            ServiceStopError::Internal(err) => write!(f, "unable to stop service: {}", err),
            ServiceStopError::PoisonedLock(msg) => write!(f, "a lock was poisoned: {}", msg),
        }
    }
}

impl From<ServiceDisconnectionError> for ServiceStopError {
    fn from(err: ServiceDisconnectionError) -> Self {
        ServiceStopError::UnableToDisconnect(err)
    }
}

#[derive(Debug)]
pub enum ServiceDestroyError {
    NotStopped,
    Internal(Box<dyn Error + Send>),
    PoisonedLock(String),
}

impl Error for ServiceDestroyError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ServiceDestroyError::NotStopped => None,
            ServiceDestroyError::Internal(err) => Some(&**err),
            ServiceDestroyError::PoisonedLock(_) => None,
        }
    }
}

impl std::fmt::Display for ServiceDestroyError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ServiceDestroyError::NotStopped => write!(f, "service not stopped"),
            ServiceDestroyError::Internal(err) => write!(f, "unable to destroy service: {}", err),
            ServiceDestroyError::PoisonedLock(msg) => write!(f, "a lock was poisoned: {}", msg),
        }
    }
}

#[derive(Debug)]
pub enum ServiceError {
    /// Returned if an error is detected when creating a service
    UnableToCreate(Box<dyn Error + Send>),
    /// Returned if an error is detected when parsing a message
    InvalidMessageFormat(Box<dyn Error + Send>),
    /// Returned if an error is detected during the handling of a message
    UnableToHandleMessage(Box<dyn Error + Send>),
    /// Returned if an error occurs during the sending of an outbound message
    UnableToSendMessage(Box<ServiceSendError>),

    /// Returned if a service encounters a poisoned lock and is unable to recover
    PoisonedLock(String),

    /// Returned if handle_message is called when not yet registered.
    NotStarted,
}

impl Error for ServiceError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ServiceError::UnableToCreate(err) => Some(&**err),
            ServiceError::InvalidMessageFormat(err) => Some(&**err),
            ServiceError::UnableToHandleMessage(err) => Some(&**err),
            ServiceError::UnableToSendMessage(err) => Some(err),
            ServiceError::PoisonedLock(_) => None,
            ServiceError::NotStarted => None,
        }
    }
}

impl std::fmt::Display for ServiceError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            ServiceError::UnableToCreate(ref err) => {
                write!(f, "service was unable to be created: {}", err)
            }
            ServiceError::InvalidMessageFormat(ref err) => {
                write!(f, "message is in an invalid format: {}", err)
            }
            ServiceError::UnableToHandleMessage(ref err) => {
                write!(f, "cannot handle message {}", err)
            }
            ServiceError::UnableToSendMessage(ref err) => {
                write!(f, "unable to send message: {}", err)
            }
            ServiceError::PoisonedLock(ref msg) => write!(f, "a lock was poisoned: {}", msg),
            ServiceError::NotStarted => f.write_str("service not started"),
        }
    }
}

impl From<ProtobufError> for ServiceError {
    fn from(err: ProtobufError) -> Self {
        ServiceError::InvalidMessageFormat(Box::new(err))
    }
}

impl From<ServiceSendError> for ServiceError {
    fn from(err: ServiceSendError) -> Self {
        ServiceError::UnableToSendMessage(Box::new(err))
    }
}

#[derive(Debug)]
pub enum FactoryCreateError {
    CreationFailed(Box<dyn Error + Send>),
    InvalidArguments(String),
}

impl Error for FactoryCreateError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            FactoryCreateError::CreationFailed(err) => Some(&**err),
            FactoryCreateError::InvalidArguments(_) => None,
        }
    }
}

impl std::fmt::Display for FactoryCreateError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            FactoryCreateError::CreationFailed(err) => {
                write!(f, "failed to create service: {}", err)
            }
            FactoryCreateError::InvalidArguments(err) => {
                write!(f, "invalid arguments specified: {}", err)
            }
        }
    }
}

#[derive(Debug)]
pub enum ServiceProcessorError {
    /// Returned if an error is detected adding a new service
    AddServiceError(String),
    /// Returned if an error is detected while processing requests
    ProcessError(String, Box<dyn Error + Send>),
    /// Returned if an IO error is detected while processing requests
    IOError(IOError),
    /// Returned if an error is detected when trying to shutdown
    ShutdownError(String),
}

impl Error for ServiceProcessorError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ServiceProcessorError::AddServiceError(_) => None,
            ServiceProcessorError::ProcessError(_, err) => Some(&**err),
            ServiceProcessorError::IOError(err) => Some(err),
            ServiceProcessorError::ShutdownError(_) => None,
        }
    }
}

impl std::fmt::Display for ServiceProcessorError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            ServiceProcessorError::AddServiceError(ref err) => {
                write!(f, "service cannot be added: {}", err)
            }
            ServiceProcessorError::ProcessError(ref ctx, ref err) => {
                write!(f, "error processing message: {} ({})", ctx, err)
            }
            ServiceProcessorError::IOError(ref err) => {
                write!(f, "io error processing message {}", err)
            }
            ServiceProcessorError::ShutdownError(ref err) => {
                write!(f, "error shutting down: {}", err)
            }
        }
    }
}

impl From<IOError> for ServiceProcessorError {
    fn from(error: IOError) -> Self {
        ServiceProcessorError::IOError(error)
    }
}