aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/src/error.rs
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-06-15 00:02:19 +0200
committerhozan23 <hozan23@karyontech.net>2024-06-15 00:02:19 +0200
commit5064133f1f59be9539ff6a2ebd830132b2379564 (patch)
tree3722421bea0b3dd21137f9abc98e3e6c950d76d8 /jsonrpc/src/error.rs
parent3429caa87699d986f799a11f6e0f4526e723b655 (diff)
jsonrpc: separate the RPC errors from the library implementation errors
Diffstat (limited to 'jsonrpc/src/error.rs')
-rw-r--r--jsonrpc/src/error.rs44
1 files changed, 31 insertions, 13 deletions
diff --git a/jsonrpc/src/error.rs b/jsonrpc/src/error.rs
index 3994bcf..89d0e2f 100644
--- a/jsonrpc/src/error.rs
+++ b/jsonrpc/src/error.rs
@@ -14,21 +14,12 @@ pub enum Error {
#[error("Subscribe Error: code: {0} msg: {1}")]
SubscribeError(i32, String),
- #[error("RPC Method Error: code: {0} msg: {1}")]
- RPCMethodError(i32, &'static str),
-
- #[error("Invalid Params: {0}")]
- InvalidParams(&'static str),
-
- #[error("Invalid Request: {0}")]
- InvalidRequest(&'static str),
+ #[error("Invalid Message Error: {0}")]
+ InvalidMsg(&'static str),
#[error(transparent)]
ParseJSON(#[from] serde_json::Error),
- #[error("Invalid Message Error: {0}")]
- InvalidMsg(&'static str),
-
#[error("Unsupported protocol: {0}")]
UnsupportedProtocol(String),
@@ -42,7 +33,7 @@ pub enum Error {
ChannelRecv(#[from] async_channel::RecvError),
#[error("Channel send Error: {0}")]
- ChannelSend(String),
+ ChannelSend(&'static str),
#[error("Unexpected Error: {0}")]
General(&'static str),
@@ -56,6 +47,33 @@ pub enum Error {
impl<T> From<async_channel::SendError<T>> for Error {
fn from(error: async_channel::SendError<T>) -> Self {
- Error::ChannelSend(error.to_string())
+ Error::ChannelSend(error.to_string().leak())
+ }
+}
+
+pub type RPCResult<T> = std::result::Result<T, RPCError>;
+
+/// Represents RPC Error.
+#[derive(ThisError, Debug)]
+pub enum RPCError {
+ #[error("Custom Error: code: {0} msg: {1}")]
+ CustomError(i32, &'static str),
+
+ #[error("Invalid Params: {0}")]
+ InvalidParams(&'static str),
+
+ #[error("Invalid Request: {0}")]
+ InvalidRequest(&'static str),
+
+ #[error("Parse Error: {0}")]
+ ParseError(&'static str),
+
+ #[error("Internal Error")]
+ InternalError,
+}
+
+impl From<serde_json::Error> for RPCError {
+ fn from(error: serde_json::Error) -> Self {
+ RPCError::ParseError(error.to_string().leak())
}
}