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
use std::fmt;
use crate::rctx::RCtxState;
/// Module-specific error codes.
#[derive(Debug)]
pub enum Error<E> {
/// The server object has shut down.
///
/// This happens when clients:
/// - attempt to send messages to a server that has been deallocated.
/// - have their requests dropped from the serrver's queue because the
/// server itself was deallocated.
ServerDisappeared,
/// All the client objects have shut down.
///
/// This happens when a server attempts to pull the latest node on off the
/// queue, but there are no more nodes on the queue and all the associated
/// client objects have been released (impled: New nodes can never be added
/// to the queue).
ClientsDisappeared,
/// The message was delivered to the server, but the reply context was
/// released before sending back a reply.
NoReply,
/// Application-specific error.
/// The `E` type is typically declared as the third generic parameter to
/// [`channel`](crate::channel()).
App(E)
}
impl<E> Error<E> {
pub fn into_apperr(self) -> Option<E> {
match self {
Error::App(e) => Some(e),
_ => None
}
}
pub fn unwrap_apperr(self) -> E {
match self {
Error::App(e) => e,
_ => panic!("Not an Error::App")
}
}
}
impl<E: fmt::Debug> std::error::Error for Error<E> {}
impl<E: fmt::Debug> fmt::Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::ServerDisappeared => write!(f, "Server disappeared"),
Error::ClientsDisappeared => write!(f, "Clients disappeared"),
Error::NoReply => write!(f, "Server didn't reply"),
Error::App(err) => write!(f, "Application error; {:?}", err)
}
}
}
impl<E> From<swctx::Error<RCtxState, E>> for Error<E> {
fn from(err: swctx::Error<RCtxState, E>) -> Self {
match err {
swctx::Error::Aborted(state) => match state {
RCtxState::Queued => Error::ServerDisappeared,
RCtxState::Active => Error::NoReply
},
swctx::Error::App(e) => Error::App(e)
}
}
}
// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :