-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement support for custom errors * Remove unneded for<'a> from E bound * Fix doctest * Handle the case where there are not exactly two arguments * Support for other Result paths * Rewrite with a more explicit rewriting logic * Back to rewriting the error argument * Add UI error for non-result * Apply suggestions from code review Co-authored-by: Niklas Adolfsson <[email protected]> * Fix a typo * Fix errors in the rest of the targets Co-authored-by: Niklas Adolfsson <[email protected]>
- Loading branch information
Showing
14 changed files
with
179 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Example of using custom errors. | ||
use std::net::SocketAddr; | ||
|
||
use jsonrpsee::core::async_trait; | ||
use jsonrpsee::proc_macros::rpc; | ||
use jsonrpsee::server::ServerBuilder; | ||
use jsonrpsee::ws_client::*; | ||
|
||
pub enum CustomError { | ||
One, | ||
Two { custom_data: u32 }, | ||
} | ||
|
||
impl From<CustomError> for jsonrpsee::core::Error { | ||
fn from(err: CustomError) -> Self { | ||
let code = match &err { | ||
CustomError::One => 101, | ||
CustomError::Two { .. } => 102, | ||
}; | ||
let data = match &err { | ||
CustomError::One => None, | ||
CustomError::Two { custom_data } => Some(serde_json::json!({ "customData": custom_data })), | ||
}; | ||
|
||
let data = data.map(|val| serde_json::value::to_raw_value(&val).unwrap()); | ||
|
||
let error_object = jsonrpsee::types::ErrorObjectOwned::owned(code, "custom_error", data); | ||
|
||
Self::Call(jsonrpsee::types::error::CallError::Custom(error_object)) | ||
} | ||
} | ||
|
||
#[rpc(client, server, namespace = "foo")] | ||
pub trait Rpc { | ||
#[method(name = "method1")] | ||
async fn method1(&self) -> Result<u16, CustomError>; | ||
|
||
#[method(name = "method2")] | ||
async fn method2(&self) -> Result<u16, CustomError>; | ||
} | ||
|
||
pub struct RpcServerImpl; | ||
|
||
#[async_trait] | ||
impl RpcServer for RpcServerImpl { | ||
async fn method1(&self) -> Result<u16, CustomError> { | ||
Err(CustomError::One) | ||
} | ||
|
||
async fn method2(&self) -> Result<u16, CustomError> { | ||
Err(CustomError::Two { custom_data: 123 }) | ||
} | ||
} | ||
|
||
pub async fn server() -> SocketAddr { | ||
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap(); | ||
let addr = server.local_addr().unwrap(); | ||
let server_handle = server.start(RpcServerImpl.into_rpc()).unwrap(); | ||
|
||
tokio::spawn(server_handle.stopped()); | ||
|
||
addr | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let server_addr = server().await; | ||
let server_url = format!("ws://{}", server_addr); | ||
let client = WsClientBuilder::default().build(&server_url).await.unwrap(); | ||
|
||
let get_error_object = |err| match err { | ||
jsonrpsee::core::Error::Call(jsonrpsee::types::error::CallError::Custom(object)) => object, | ||
_ => panic!("wrong error kind: {:?}", err), | ||
}; | ||
|
||
let error = client.method1().await.unwrap_err(); | ||
let error_object = get_error_object(error); | ||
assert_eq!(error_object.code(), 101); | ||
assert_eq!(error_object.message(), "custom_error"); | ||
assert!(error_object.data().is_none()); | ||
|
||
let error = client.method2().await.unwrap_err(); | ||
let error_object = get_error_object(error); | ||
assert_eq!(error_object.code(), 102); | ||
assert_eq!(error_object.message(), "custom_error"); | ||
assert_eq!(error_object.data().unwrap().get(), r#"{"customData":123}"#); | ||
} |
9 changes: 9 additions & 0 deletions
9
proc-macros/tests/ui/incorrect/method/method_non_result_return_type.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use jsonrpsee::proc_macros::rpc; | ||
|
||
#[rpc(client)] | ||
pub trait NonResultReturnType { | ||
#[method(name = "a")] | ||
async fn a(&self) -> u16; | ||
} | ||
|
||
fn main() {} |
5 changes: 5 additions & 0 deletions
5
proc-macros/tests/ui/incorrect/method/method_non_result_return_type.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: Expecting something like 'Result<Foo, Err>' here, but got no generic args (eg no '<Foo,Err>'). | ||
--> tests/ui/incorrect/method/method_non_result_return_type.rs:6:23 | ||
| | ||
6 | async fn a(&self) -> u16; | ||
| ^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters