From 5064133f1f59be9539ff6a2ebd830132b2379564 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sat, 15 Jun 2024 00:02:19 +0200 Subject: jsonrpc: separate the RPC errors from the library implementation errors --- jsonrpc/examples/pubsub_server.rs | 12 +++++++----- jsonrpc/examples/server.rs | 10 +++++----- jsonrpc/examples/tokio_server/src/main.rs | 16 +++++++++------- 3 files changed, 21 insertions(+), 17 deletions(-) (limited to 'jsonrpc/examples') diff --git a/jsonrpc/examples/pubsub_server.rs b/jsonrpc/examples/pubsub_server.rs index 40ea756..d1623c1 100644 --- a/jsonrpc/examples/pubsub_server.rs +++ b/jsonrpc/examples/pubsub_server.rs @@ -5,7 +5,9 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use karyon_core::async_util::sleep; -use karyon_jsonrpc::{message::SubscriptionID, rpc_impl, rpc_pubsub_impl, Channel, Error, Server}; +use karyon_jsonrpc::{ + message::SubscriptionID, rpc_impl, rpc_pubsub_impl, Channel, RPCError, Server, +}; struct Calc {} @@ -20,7 +22,7 @@ struct Pong {} #[rpc_impl] impl Calc { - async fn ping(&self, _params: Value) -> Result { + async fn ping(&self, _params: Value) -> Result { Ok(serde_json::json!(Pong {})) } } @@ -32,14 +34,14 @@ impl Calc { chan: Arc, method: String, _params: Value, - ) -> Result { + ) -> Result { let sub = chan.new_subscription(&method).await; let sub_id = sub.id.clone(); smol::spawn(async move { loop { sleep(Duration::from_millis(500)).await; if let Err(err) = sub.notify(serde_json::json!("Hello")).await { - error!("Error send notification {err}"); + error!("Send notification {err}"); break; } } @@ -54,7 +56,7 @@ impl Calc { chan: Arc, _method: String, params: Value, - ) -> Result { + ) -> Result { let sub_id: SubscriptionID = serde_json::from_value(params)?; chan.remove_subscription(&sub_id).await; Ok(serde_json::json!(true)) diff --git a/jsonrpc/examples/server.rs b/jsonrpc/examples/server.rs index 31e65dd..acbe2a9 100644 --- a/jsonrpc/examples/server.rs +++ b/jsonrpc/examples/server.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use karyon_core::async_util::sleep; -use karyon_jsonrpc::{rpc_impl, Error, Server}; +use karyon_jsonrpc::{rpc_impl, RPCError, Server}; struct Calc { version: String, @@ -21,21 +21,21 @@ struct Pong {} #[rpc_impl] impl Calc { - async fn ping(&self, _params: Value) -> Result { + async fn ping(&self, _params: Value) -> Result { Ok(serde_json::json!(Pong {})) } - async fn add(&self, params: Value) -> Result { + async fn add(&self, params: Value) -> Result { let params: Req = serde_json::from_value(params)?; Ok(serde_json::json!(params.x + params.y)) } - async fn sub(&self, params: Value) -> Result { + async fn sub(&self, params: Value) -> Result { let params: Req = serde_json::from_value(params)?; Ok(serde_json::json!(params.x - params.y)) } - async fn version(&self, _params: Value) -> Result { + async fn version(&self, _params: Value) -> Result { Ok(serde_json::json!(self.version)) } } diff --git a/jsonrpc/examples/tokio_server/src/main.rs b/jsonrpc/examples/tokio_server/src/main.rs index a9b2b32..5a8d604 100644 --- a/jsonrpc/examples/tokio_server/src/main.rs +++ b/jsonrpc/examples/tokio_server/src/main.rs @@ -3,7 +3,9 @@ use std::{sync::Arc, time::Duration}; use serde::{Deserialize, Serialize}; use serde_json::Value; -use karyon_jsonrpc::{message::SubscriptionID, rpc_impl, rpc_pubsub_impl, Channel, Error, Server}; +use karyon_jsonrpc::{ + message::SubscriptionID, rpc_impl, rpc_pubsub_impl, Channel, RPCError, Server, +}; struct Calc { version: String, @@ -20,21 +22,21 @@ struct Pong {} #[rpc_impl] impl Calc { - async fn ping(&self, _params: Value) -> Result { + async fn ping(&self, _params: Value) -> Result { Ok(serde_json::json!(Pong {})) } - async fn add(&self, params: Value) -> Result { + async fn add(&self, params: Value) -> Result { let params: Req = serde_json::from_value(params)?; Ok(serde_json::json!(params.x + params.y)) } - async fn sub(&self, params: Value) -> Result { + async fn sub(&self, params: Value) -> Result { let params: Req = serde_json::from_value(params)?; Ok(serde_json::json!(params.x - params.y)) } - async fn version(&self, _params: Value) -> Result { + async fn version(&self, _params: Value) -> Result { Ok(serde_json::json!(self.version)) } } @@ -46,7 +48,7 @@ impl Calc { chan: Arc, method: String, _params: Value, - ) -> Result { + ) -> Result { let sub = chan.new_subscription(&method).await; let sub_id = sub.id; tokio::spawn(async move { @@ -66,7 +68,7 @@ impl Calc { chan: Arc, _method: String, params: Value, - ) -> Result { + ) -> Result { let sub_id: SubscriptionID = serde_json::from_value(params)?; chan.remove_subscription(&sub_id).await; Ok(serde_json::json!(true)) -- cgit v1.2.3