qrpc_sdk/
error.rs

1//! RPC related error handling
2
3use std::fmt::{self, Display, Formatter};
4
5pub type RpcResult<T> = Result<T, RpcError>;
6
7/// A set of errors that occur when connecting to services
8#[derive(Debug)]
9pub enum RpcError {
10    /// No such service was found by the broker
11    NoSuchService(String),
12    /// The selected recipient didn't reply within the timeout
13    ///
14    /// This may indicate that the requested service has crashed, is
15    /// dealing with backpressure, or the broker is quietly dropping
16    /// requests.
17    Timeout,
18    /// Tried connecting to a service that's already connected
19    AlreadyConnected,
20    /// Failed to perform action that requires a connection
21    NotConnected,
22    /// Invalid connection: performing the last operation has failed
23    ConnectionFault(String),
24    /// Encoding or decoding a payload failed
25    EncoderFault(String),
26    /// Any other failure with it's error message string
27    Other(String),
28}
29
30impl Display for RpcError {
31    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
32        write!(
33            f,
34            "{}",
35            match self {
36                Self::NoSuchService(s) => format!("The requested service {} does not exist!", s),
37                Self::Timeout => "The requested operation took too long (timeout)!".into(),
38                Self::AlreadyConnected =>
39                    "Tried connecting to an already connected component!".into(),
40                Self::NotConnected =>
41                    "Tried to perform an action that needs a component connection!".into(),
42                Self::ConnectionFault(s) => format!("I/O error: {}", s),
43                Self::EncoderFault(s) => format!("Encode error: {}", s),
44                Self::Other(s) => format!("Unknown error: {}", s),
45            }
46        )
47    }
48}
49
50impl From<std::io::Error> for RpcError {
51    fn from(e: std::io::Error) -> Self {
52        Self::ConnectionFault(e.to_string())
53    }
54}
55
56impl From<capnp::Error> for RpcError {
57    fn from(e: capnp::Error) -> Self {
58        Self::EncoderFault(e.to_string())
59    }
60}
61
62impl From<async_std::future::TimeoutError> for RpcError {
63    fn from(_: async_std::future::TimeoutError) -> Self {
64        Self::Timeout
65    }
66}