xrpc/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum RpcError {
5    #[error("Serialization error: {0}")]
6    Serialization(String),
7
8    #[error("Invalid message: {0}")]
9    InvalidMessage(String),
10
11    #[error("Method not found: {0}")]
12    MethodNotFound(String),
13
14    #[error("Request timeout: {0}")]
15    Timeout(String),
16
17    #[error("Server error: {0}")]
18    ServerError(String),
19
20    #[error("Client error: {0}")]
21    ClientError(String),
22
23    #[error("Transport error: {0}")]
24    Transport(#[from] TransportError),
25
26    #[error("Stream error: {0}")]
27    StreamError(String),
28
29    #[error("Connection closed")]
30    ConnectionClosed,
31}
32
33#[derive(Error, Debug)]
34pub enum TransportError {
35    #[error("Message too large: {size} bytes (max: {max})")]
36    MessageTooLarge { size: usize, max: usize },
37
38    #[error("Protocol error: {0}")]
39    Protocol(String),
40
41    #[error("Send failed after {attempts} attempts: {reason}")]
42    SendFailed { attempts: usize, reason: String },
43
44    #[error("Receive failed after {attempts} attempts: {reason}")]
45    ReceiveFailed { attempts: usize, reason: String },
46
47    #[error("Timeout after {duration_ms}ms: {operation}")]
48    Timeout { duration_ms: u64, operation: String },
49
50    #[error("Connection to '{name}' failed after {attempts} attempts: {reason}")]
51    ConnectionFailed {
52        name: String,
53        attempts: usize,
54        reason: String,
55    },
56
57    #[error("Not connected")]
58    NotConnected,
59
60    #[error("Invalid buffer state: {0}")]
61    InvalidBufferState(String),
62
63    #[error("Shared memory creation failed for '{name}': {reason}")]
64    SharedMemoryCreation { name: String, reason: String },
65}
66
67pub type Result<T> = std::result::Result<T, RpcError>;
68pub type TransportResult<T> = std::result::Result<T, TransportError>;
69
70impl From<bincode::Error> for RpcError {
71    fn from(err: bincode::Error) -> Self {
72        RpcError::Serialization(err.to_string())
73    }
74}
75
76impl From<serde_json::Error> for RpcError {
77    fn from(err: serde_json::Error) -> Self {
78        RpcError::Serialization(err.to_string())
79    }
80}