Unverified Commit 9f88eb81 authored by Niklas Adolfsson's avatar Niklas Adolfsson Committed by GitHub
Browse files

fix: `RpcModule::call` decode response correctly (#839)

Closing https://github.com/paritytech/jsonrpsee/issues/838
parent 4a7d7252
Pipeline #205952 passed with stages
in 4 minutes and 49 seconds
...@@ -367,17 +367,14 @@ impl Methods { ...@@ -367,17 +367,14 @@ impl Methods {
tracing::trace!("[Methods::call] Calling method: {:?}, params: {:?}", method, params); tracing::trace!("[Methods::call] Calling method: {:?}, params: {:?}", method, params);
let (resp, _, _) = self.inner_call(req).await; let (resp, _, _) = self.inner_call(req).await;
let res = match serde_json::from_str::<Response<T>>(&resp.result) { if resp.success {
Ok(res) => Ok(res.result), serde_json::from_str::<Response<T>>(&resp.result).map(|r| r.result).map_err(Into::into)
Err(e) => {
if let Ok(err) = serde_json::from_str::<ErrorResponse>(&resp.result) {
Err(Error::Call(CallError::Custom(err.error_object().clone().into_owned())))
} else { } else {
Err(e.into()) match serde_json::from_str::<ErrorResponse>(&resp.result) {
Ok(err) => Err(Error::Call(CallError::Custom(err.error_object().clone().into_owned()))),
Err(e) => Err(e.into()),
} }
} }
};
res
} }
/// Make a request (JSON-RPC method call or subscription) by using raw JSON. /// Make a request (JSON-RPC method call or subscription) by using raw JSON.
......
...@@ -166,6 +166,10 @@ async fn calling_method_without_server_using_proc_macro() { ...@@ -166,6 +166,10 @@ async fn calling_method_without_server_using_proc_macro() {
/// Async method. /// Async method.
#[method(name = "revolution")] #[method(name = "revolution")]
async fn can_have_any_name(&self, beverage: Beverage, some_bytes: Vec<u8>) -> Result<String, Error>; async fn can_have_any_name(&self, beverage: Beverage, some_bytes: Vec<u8>) -> Result<String, Error>;
/// Async method with option.
#[method(name = "can_have_options")]
async fn can_have_options(&self, x: usize) -> Result<Option<String>, Error>;
} }
struct CoolServerImpl; struct CoolServerImpl;
...@@ -183,6 +187,14 @@ async fn calling_method_without_server_using_proc_macro() { ...@@ -183,6 +187,14 @@ async fn calling_method_without_server_using_proc_macro() {
async fn can_have_any_name(&self, beverage: Beverage, some_bytes: Vec<u8>) -> Result<String, Error> { async fn can_have_any_name(&self, beverage: Beverage, some_bytes: Vec<u8>) -> Result<String, Error> {
Ok(format!("drink: {:?}, phases: {:?}", beverage, some_bytes)) Ok(format!("drink: {:?}, phases: {:?}", beverage, some_bytes))
} }
async fn can_have_options(&self, x: usize) -> Result<Option<String>, Error> {
match x {
0 => Ok(Some("one".to_string())),
1 => Ok(None),
_ => Err(Error::Custom("too big number".to_string())),
}
}
} }
let module = CoolServerImpl.into_rpc(); let module = CoolServerImpl.into_rpc();
...@@ -203,6 +215,20 @@ async fn calling_method_without_server_using_proc_macro() { ...@@ -203,6 +215,20 @@ async fn calling_method_without_server_using_proc_macro() {
// Call async method with params and context // Call async method with params and context
let result: String = module.call("revolution", (Beverage { ice: true }, vec![1, 2, 3])).await.unwrap(); let result: String = module.call("revolution", (Beverage { ice: true }, vec![1, 2, 3])).await.unwrap();
assert_eq!(&result, "drink: Beverage { ice: true }, phases: [1, 2, 3]"); assert_eq!(&result, "drink: Beverage { ice: true }, phases: [1, 2, 3]");
// Call async method with option which is `Some`
let result: Option<String> = module.call("can_have_options", vec![0]).await.unwrap();
assert_eq!(result, Some("one".to_string()));
// Call async method with option which is `None`
let result: Option<String> = module.call("can_have_options", vec![1]).await.unwrap();
assert_eq!(result, None);
// Call async method with option which should `Err`.
let err = module.call::<_, Option<String>>("can_have_options", vec![2]).await.unwrap_err();
assert!(matches!(err,
Error::Call(CallError::Custom(err)) if err.message() == "Custom error: too big number"
));
} }
#[tokio::test] #[tokio::test]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment