Skip to main content

karyon_jsonrpc/
error.rs

1use thiserror::Error as ThisError;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5/// Represents karyon's jsonrpc Error.
6#[derive(ThisError, Debug)]
7pub enum Error {
8    #[error(transparent)]
9    IO(#[from] std::io::Error),
10
11    #[error("Call Error: code: {0} msg: {1}")]
12    CallError(i32, String),
13
14    #[error("Subscribe Error: code: {0} msg: {1}")]
15    SubscribeError(i32, String),
16
17    #[error("Encode Error: {0}")]
18    Encode(String),
19
20    #[error("Decode Error: {0}")]
21    Decode(String),
22
23    #[error("Invalid Message: {0}")]
24    InvalidMsg(String),
25
26    #[error("Invalid State: {0}")]
27    InvalidState(String),
28
29    #[error("Buffer Full: {0}")]
30    BufferFull(String),
31
32    #[error(transparent)]
33    ParseJSON(#[from] serde_json::Error),
34
35    #[error("Unsupported Protocol: {0}")]
36    UnsupportedProtocol(String),
37
38    #[error("TLS config is required")]
39    TLSConfigRequired,
40
41    #[cfg(feature = "quic")]
42    #[error("QUIC config is required")]
43    QUICConfigRequired,
44
45    #[error("HTTP Error: {0}")]
46    HttpError(String),
47
48    #[error("Connection Closed: {0}")]
49    CloseConnection(String),
50
51    #[error("Subscription Not Found: {0}")]
52    SubscriptionNotFound(String),
53
54    #[error("Subscription Buffer Full")]
55    SubscriptionBufferFull,
56
57    #[error("Subscription Closed")]
58    SubscriptionClosed,
59
60    #[error("Subscription Duplicated: {0}")]
61    SubscriptionDuplicated(String),
62
63    #[error("Client Disconnected")]
64    ClientDisconnected,
65
66    #[error(transparent)]
67    ChannelRecv(#[from] async_channel::RecvError),
68
69    #[error("Channel Send Error: {0}")]
70    ChannelSend(String),
71
72    #[cfg(feature = "ws")]
73    #[error(transparent)]
74    WebSocket(#[from] async_tungstenite::tungstenite::Error),
75
76    #[error("Unexpected Error: {0}")]
77    General(String),
78
79    #[error(transparent)]
80    KaryonCore(#[from] karyon_core::error::Error),
81
82    #[error(transparent)]
83    KaryonNet(#[from] karyon_net::Error),
84}
85
86impl<T> From<async_channel::SendError<T>> for Error {
87    fn from(error: async_channel::SendError<T>) -> Self {
88        Error::ChannelSend(error.to_string())
89    }
90}
91
92pub type RPCResult<T> = std::result::Result<T, RPCError>;
93
94/// Represents RPC Error.
95#[derive(ThisError, Debug)]
96pub enum RPCError {
97    #[error("Custom Error: code: {0} msg: {1}")]
98    CustomError(i32, String),
99
100    #[error("Invalid Params: {0}")]
101    InvalidParams(String),
102
103    #[error("Invalid Request: {0}")]
104    InvalidRequest(String),
105
106    #[error("Parse Error: {0}")]
107    ParseError(String),
108
109    #[error("Internal Error")]
110    InternalError,
111}
112
113impl From<serde_json::Error> for RPCError {
114    fn from(error: serde_json::Error) -> Self {
115        RPCError::ParseError(error.to_string())
116    }
117}