Skip to main content

libcfd_rpc/
error.rs

1use thiserror::Error;
2
3/// Errors surfaced by the Cap'n Proto RPC layer.
4#[derive(Debug, Error)]
5pub enum RpcError {
6    /// The stream ended while a full RPC message was expected.
7    #[error("rpc stream ended unexpectedly")]
8    Eof,
9    /// Invalid or malformed Cap'n Proto framing or message content.
10    #[error("rpc protocol error: {0}")]
11    Protocol(String),
12    /// The peer aborted the RPC connection.
13    #[error("rpc aborted by peer (type {error_type}): {reason}")]
14    Abort {
15        /// The abort reason reported by the peer.
16        reason: String,
17        /// The abort type code reported by the peer.
18        error_type: u16,
19    },
20    /// The peer returned an exception for a call we made.
21    #[error("rpc call failed remotely: {0}")]
22    RemoteCall(String),
23    /// The transport failed while reading or writing.
24    #[error("rpc transport error: {0}")]
25    Io(#[from] std::io::Error),
26}
27
28impl From<capnp::Error> for RpcError {
29    fn from(error: capnp::Error) -> Self {
30        Self::Protocol(error.to_string())
31    }
32}
33
34impl From<capnp::NotInSchema> for RpcError {
35    fn from(error: capnp::NotInSchema) -> Self {
36        Self::Protocol(format!("value not in schema: {error:?}"))
37    }
38}
39
40impl From<std::str::Utf8Error> for RpcError {
41    fn from(error: std::str::Utf8Error) -> Self {
42        Self::Protocol(format!("invalid utf-8 in message: {error}"))
43    }
44}
45
46pub(crate) type Result<T> = std::result::Result<T, RpcError>;