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
use serde::{Deserialize, Serialize};

/// An error that can occur in the processing of an RPC. This is not request-specific errors but
/// rather cross-cutting errors that can always occur.
#[derive(thiserror::Error, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum RpcError {
    /// The request exceeded its deadline.
    #[error("the request exceeded its deadline: {0}")]
    DeadlineExceeded(String),

    /// A capability provider was called before its configure_dispatch was called.
    #[error("the capability provider has not been initialized: {0}")]
    NotInitialized(String),

    #[error("method not handled {0}")]
    MethodNotHandled(String),

    /// Error that can be returned if server has not implemented
    /// an optional interface method
    #[error("method not implemented")]
    NotImplemented,

    #[error("Host send error {0}")]
    HostError(String),

    #[error("deserialization: {0}")]
    Deser(String),

    #[error("serialization: {0}")]
    Ser(String),

    #[error("rpc: {0}")]
    Rpc(String),

    #[error("nats: {0}")]
    Nats(String),

    #[error("invalid parameter: {0}")]
    InvalidParameter(String),

    /// Error occurred in actor's rpc handler
    #[error("actor: {0}")]
    ActorHandler(String),

    /// Error occurred during provider initialization or put-link
    #[error("provider initialization or put-link: {0}")]
    ProviderInit(String),

    /// Timeout occurred
    #[error("timeout: {0}")]
    Timeout(String),

    //#[error("IO error")]
    //IO([from] std::io::Error)
    /// Anything else
    #[error("{0}")]
    Other(String),
}

pub type RpcResult<T> = std::result::Result<T, RpcError>;

impl From<String> for RpcError {
    fn from(s: String) -> RpcError {
        RpcError::Other(s)
    }
}

impl From<&str> for RpcError {
    fn from(s: &str) -> RpcError {
        RpcError::Other(s.to_string())
    }
}

impl From<std::io::Error> for RpcError {
    fn from(e: std::io::Error) -> RpcError {
        RpcError::Other(format!("io: {}", e))
    }
}

impl<E> core::convert::From<minicbor::encode::Error<E>> for RpcError {
    fn from(e: minicbor::encode::Error<E>) -> Self {
        let msg = match e {
            minicbor::encode::Error::Write(_) => "writing to buffer",
            minicbor::encode::Error::Message(s) => s,
            _ => "unspecified encoding error",
        }
        .to_string();
        RpcError::Ser(format!("encode: {}", msg))
    }
}

impl core::convert::From<minicbor::decode::Error> for RpcError {
    fn from(e: minicbor::decode::Error) -> Self {
        RpcError::Ser(format!("decode: {}", e))
    }
}

/*
impl<W: minicbor::encode::Write, E: W::Error<W>>> From<minicbor::encode::Error<W::Error>> for RpcError {
    fn from(_ee: Error<<dyn minicbor::encode::Write>::Error>) -> RpcError {
        RpcError::Other("help".to_string())
    }
}
 */

/*
impl<W: std::io::Write> From<minicbor::encode::Error<W>> for RpcError {
    fn from(e: minicbor::encode::Error<W>) -> RpcError
    where
        W: minicbor::encode::Write,
    {
        RpcError::Ser(
            match e {
                minicbor::encode::Error::Write(_) => "writing to buffer",
                minicbor::encode::Error::Message(s) => s,
                _ => "unspecified encoding error",
            }
            .to_string(),
        )
    }
}
 */