Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RpcModule::call decode response correctly #839

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions core/src/server/rpc_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,14 @@ impl Methods {
tracing::trace!("[Methods::call] Calling method: {:?}, params: {:?}", method, params);
let (resp, _, _) = self.inner_call(req).await;

let res = match serde_json::from_str::<Response<T>>(&resp.result) {
Ok(res) => Ok(res.result),
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 {
Err(e.into())
}
if resp.success {
serde_json::from_str::<Response<T>>(&resp.result).map(|r| r.result).map_err(Into::into)
} else {
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.
Expand Down
26 changes: 26 additions & 0 deletions tests/tests/rpc_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ async fn calling_method_without_server_using_proc_macro() {
/// Async method.
#[method(name = "revolution")]
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;
Expand All @@ -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> {
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();

Expand All @@ -203,6 +215,20 @@ async fn calling_method_without_server_using_proc_macro() {
// Call async method with params and context
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]");

// 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]
Expand Down