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