aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'jsonrpc/src/server')
-rw-r--r--jsonrpc/src/server/mod.rs83
-rw-r--r--jsonrpc/src/server/pubsub_service.rs10
-rw-r--r--jsonrpc/src/server/service.rs10
3 files changed, 38 insertions, 65 deletions
diff --git a/jsonrpc/src/server/mod.rs b/jsonrpc/src/server/mod.rs
index 86b1b31..dd176d0 100644
--- a/jsonrpc/src/server/mod.rs
+++ b/jsonrpc/src/server/mod.rs
@@ -19,7 +19,6 @@ use response_queue::ResponseQueue;
pub const INVALID_REQUEST_ERROR_MSG: &str = "Invalid request";
pub const FAILED_TO_PARSE_ERROR_MSG: &str = "Failed to parse";
pub const METHOD_NOT_FOUND_ERROR_MSG: &str = "Method not found";
-pub const INTERNAL_ERROR_MSG: &str = "Internal error";
struct NewRequest {
srvc_name: String,
@@ -165,11 +164,15 @@ impl Server {
let rpc_msg = match serde_json::from_value::<message::Request>(request) {
Ok(m) => m,
Err(_) => {
- return SanityCheckResult::ErrRes(pack_err_res(
- message::PARSE_ERROR_CODE,
- FAILED_TO_PARSE_ERROR_MSG,
- None,
- ));
+ let response = message::Response {
+ error: Some(message::Error {
+ code: message::PARSE_ERROR_CODE,
+ message: FAILED_TO_PARSE_ERROR_MSG.to_string(),
+ data: None,
+ }),
+ ..Default::default()
+ };
+ return SanityCheckResult::ErrRes(response);
}
};
debug!("<-- {rpc_msg}");
@@ -178,11 +181,16 @@ impl Server {
let srvc_method_str = rpc_msg.method.clone();
let srvc_method: Vec<&str> = srvc_method_str.split('.').collect();
if srvc_method.len() < 2 {
- return SanityCheckResult::ErrRes(pack_err_res(
- message::INVALID_REQUEST_ERROR_CODE,
- INVALID_REQUEST_ERROR_MSG,
- Some(rpc_msg.id),
- ));
+ let response = message::Response {
+ error: Some(message::Error {
+ code: message::INVALID_REQUEST_ERROR_CODE,
+ message: INVALID_REQUEST_ERROR_MSG.to_string(),
+ data: None,
+ }),
+ id: Some(rpc_msg.id),
+ ..Default::default()
+ };
+ return SanityCheckResult::ErrRes(response);
}
let srvc_name = srvc_method[0].to_string();
@@ -235,10 +243,10 @@ impl Server {
};
let mut response = message::Response {
- jsonrpc: message::JSONRPC_VERSION.to_string(),
error: None,
result: None,
id: Some(req.msg.id.clone()),
+ ..Default::default()
};
// Check if the service exists in pubsub services list
@@ -249,7 +257,7 @@ impl Server {
let params = req.msg.params.unwrap_or(serde_json::json!(()));
response.result = match method(channel, name, params).await {
Ok(res) => Some(res),
- Err(err) => return self.handle_error(err, req.msg.id),
+ Err(err) => return err.to_response(Some(req.msg.id), None),
};
return response;
@@ -263,54 +271,19 @@ impl Server {
let params = req.msg.params.unwrap_or(serde_json::json!(()));
response.result = match method(params).await {
Ok(res) => Some(res),
- Err(err) => return self.handle_error(err, req.msg.id),
+ Err(err) => return err.to_response(Some(req.msg.id), None),
};
return response;
}
}
- pack_err_res(
- message::METHOD_NOT_FOUND_ERROR_CODE,
- METHOD_NOT_FOUND_ERROR_MSG,
- Some(req.msg.id),
- )
- }
-
- fn handle_error(&self, err: Error, msg_id: serde_json::Value) -> message::Response {
- match err {
- Error::ParseJSON(_) => pack_err_res(
- message::PARSE_ERROR_CODE,
- FAILED_TO_PARSE_ERROR_MSG,
- Some(msg_id),
- ),
- Error::InvalidParams(msg) => {
- pack_err_res(message::INVALID_PARAMS_ERROR_CODE, msg, Some(msg_id))
- }
- Error::InvalidRequest(msg) => {
- pack_err_res(message::INVALID_REQUEST_ERROR_CODE, msg, Some(msg_id))
- }
- Error::RPCMethodError(code, msg) => pack_err_res(code, msg, Some(msg_id)),
- _ => pack_err_res(
- message::INTERNAL_ERROR_CODE,
- INTERNAL_ERROR_MSG,
- Some(msg_id),
- ),
- }
- }
-}
+ response.error = Some(message::Error {
+ code: message::METHOD_NOT_FOUND_ERROR_CODE,
+ message: METHOD_NOT_FOUND_ERROR_MSG.to_string(),
+ data: None,
+ });
-fn pack_err_res(code: i32, msg: &str, id: Option<serde_json::Value>) -> message::Response {
- let err = message::Error {
- code,
- message: msg.to_string(),
- data: None,
- };
-
- message::Response {
- jsonrpc: message::JSONRPC_VERSION.to_string(),
- error: Some(err),
- result: None,
- id,
+ response
}
}
diff --git a/jsonrpc/src/server/pubsub_service.rs b/jsonrpc/src/server/pubsub_service.rs
index 08d1bbb..a6b4c11 100644
--- a/jsonrpc/src/server/pubsub_service.rs
+++ b/jsonrpc/src/server/pubsub_service.rs
@@ -1,6 +1,6 @@
use std::{future::Future, pin::Pin, sync::Arc};
-use crate::Result;
+use crate::RPCResult;
use super::channel::Channel;
@@ -8,7 +8,7 @@ use super::channel::Channel;
pub type PubSubRPCMethod<'a> =
Box<dyn Fn(Arc<Channel>, String, serde_json::Value) -> PubSubRPCMethodOutput<'a> + Send + 'a>;
type PubSubRPCMethodOutput<'a> =
- Pin<Box<dyn Future<Output = Result<serde_json::Value>> + Send + Sync + 'a>>;
+ Pin<Box<dyn Future<Output = RPCResult<serde_json::Value>> + Send + Sync + 'a>>;
/// Defines the interface for an RPC service.
pub trait PubSubRPCService: Sync + Send {
@@ -23,16 +23,16 @@ pub trait PubSubRPCService: Sync + Send {
/// ```
/// use serde_json::Value;
///
-/// use karyon_jsonrpc::{Error, impl_rpc_service};
+/// use karyon_jsonrpc::{RPCError, impl_rpc_service};
///
/// struct Hello {}
///
/// impl Hello {
-/// async fn foo(&self, params: Value) -> Result<Value, Error> {
+/// async fn foo(&self, params: Value) -> Result<Value, RPCError> {
/// Ok(serde_json::json!("foo!"))
/// }
///
-/// async fn bar(&self, params: Value) -> Result<Value, Error> {
+/// async fn bar(&self, params: Value) -> Result<Value, RPCError> {
/// Ok(serde_json::json!("bar!"))
/// }
/// }
diff --git a/jsonrpc/src/server/service.rs b/jsonrpc/src/server/service.rs
index 4c8c4b8..9cc1d21 100644
--- a/jsonrpc/src/server/service.rs
+++ b/jsonrpc/src/server/service.rs
@@ -1,11 +1,11 @@
use std::{future::Future, pin::Pin};
-use crate::Result;
+use crate::RPCResult;
/// Represents the RPC method
pub type RPCMethod<'a> = Box<dyn Fn(serde_json::Value) -> RPCMethodOutput<'a> + Send + 'a>;
type RPCMethodOutput<'a> =
- Pin<Box<dyn Future<Output = Result<serde_json::Value>> + Send + Sync + 'a>>;
+ Pin<Box<dyn Future<Output = RPCResult<serde_json::Value>> + Send + Sync + 'a>>;
/// Defines the interface for an RPC service.
pub trait RPCService: Sync + Send {
@@ -20,16 +20,16 @@ pub trait RPCService: Sync + Send {
/// ```
/// use serde_json::Value;
///
-/// use karyon_jsonrpc::{Error, impl_rpc_service};
+/// use karyon_jsonrpc::{RPCError, impl_rpc_service};
///
/// struct Hello {}
///
/// impl Hello {
-/// async fn foo(&self, params: Value) -> Result<Value, Error> {
+/// async fn foo(&self, params: Value) -> Result<Value, RPCError> {
/// Ok(serde_json::json!("foo!"))
/// }
///
-/// async fn bar(&self, params: Value) -> Result<Value, Error> {
+/// async fn bar(&self, params: Value) -> Result<Value, RPCError> {
/// Ok(serde_json::json!("bar!"))
/// }
/// }