Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Apr 20, 2023
1 parent 4e14d34 commit 8f0a165
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 61 deletions.
3 changes: 1 addition & 2 deletions client/http-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ where

// NOTE: it's decoded first to `JsonRawValue` and then to `R` below to get
// a better error message if `R` couldn't be decoded.
let response = ResponseSuccess::try_from(serde_json::from_slice::<Response<&JsonRawValue>>(&body)?)
.map_err(|e| Error::Call(e))?;
let response = ResponseSuccess::try_from(serde_json::from_slice::<Response<&JsonRawValue>>(&body)?)?;

let result = serde_json::from_str(response.result.get()).map_err(Error::ParseError)?;

Expand Down
2 changes: 1 addition & 1 deletion core/src/client/async_client/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub(crate) fn process_single_response(
max_capacity_per_subscription: usize,
) -> Result<Option<RequestMessage>, Error> {
let response_id = response.id.clone().into_owned();
let result = ResponseSuccess::try_from(response).map(|s| s.result).map_err(|e| Error::Call(e));
let result = ResponseSuccess::try_from(response).map(|s| s.result).map_err(Error::Call);

match manager.request_status(&response_id) {
RequestStatus::PendingMethodCall => {
Expand Down
6 changes: 0 additions & 6 deletions core/src/server/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,6 @@ impl MethodSinkPermit {
self.send_raw(json)
}

/*
/// Helper for sending the general purpose `Error` as a JSON-RPC errors to the client.
pub fn send_call_error(self, id: Id, err: Error) {
self.send_error(id, err.into())
}*/

/// Send a raw JSON-RPC message to the client, `MethodSink` does not check the validity
/// of the JSON being sent.
pub fn send_raw(self, json: String) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/server/rpc_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl Methods {
tracing::trace!("[Methods::call] Method: {:?}, params: {:?}", method, params);
let (resp, _) = self.inner_call(req, 1, mock_subscription_permit()).await;
let rp = serde_json::from_str::<Response<T>>(&resp.result)?;
ResponseSuccess::try_from(rp).map(|s| s.result).map_err(|e| Error::Call(e))
ResponseSuccess::try_from(rp).map(|s| s.result).map_err(Error::Call)
}

/// Make a request (JSON-RPC method call or subscription) by using raw JSON.
Expand Down Expand Up @@ -386,7 +386,7 @@ impl Methods {

// TODO: hack around the lifetime on the `SubscriptionId` by deserialize first to serde_json::Value.
let as_success: ResponseSuccess<serde_json::Value> =
serde_json::from_str::<Response<_>>(&resp.result)?.try_into().map_err(|e| Error::Call(e))?;
serde_json::from_str::<Response<_>>(&resp.result)?.try_into()?;

let sub_id = as_success.result.try_into().map_err(|_| Error::InvalidSubscriptionId)?;

Expand Down
8 changes: 4 additions & 4 deletions proc-macros/src/render_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ impl RpcDescription {
return quote_spanned!(args.span() => compile_error!("RpcResult must have one argument"));
}

// Force the last argument to be `jsonrpsee::core::Error`:
let error_arg = args.last_mut().unwrap();
*error_arg = syn::GenericArgument::Type(syn::Type::Verbatim(self.jrps_client_item(quote! { core::Error })));
// The type alias `RpcResult<T>` is modified to `Result<T, Error>`.
let ret_ty = args.last_mut().unwrap();
let err_ty = self.jrps_client_item(quote! { core::Error });

quote!(#ty)
quote! { core::result::Result<#ret_ty, #err_ty> }
} else {
// Any other type name isn't allowed.
quote_spanned!(type_name.span() => compile_error!("The return type must be Result or RpcResult"))
Expand Down
4 changes: 2 additions & 2 deletions proc-macros/tests/ui/correct/custom_ret_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::net::SocketAddr;

use jsonrpsee::core::{async_trait, RpcResult, Serialize};
use jsonrpsee::core::{async_trait, Serialize};
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::server::{IntoResponse, ServerBuilder};
use jsonrpsee::types::ResponsePayload;
Expand Down Expand Up @@ -86,7 +86,7 @@ async fn main() {
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,
jsonrpsee::core::Error::Call(object) => object,
_ => panic!("wrong error kind: {:?}", err),
};

Expand Down
6 changes: 3 additions & 3 deletions proc-macros/tests/ui/correct/only_client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Example of using proc macro to generate working client.
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use jsonrpsee::{core::Error, proc_macros::rpc};

#[rpc(client)]
pub trait Rpc {
#[method(name = "foo")]
async fn async_method(&self, param_a: u8, param_b: String) -> RpcResult<u16>;
async fn async_method(&self, param_a: u8, param_b: String) -> Result<u16, Error>;

#[method(name = "bar")]
fn sync_method(&self) -> RpcResult<u16>;
fn sync_method(&self) -> Result<u16, Error>;

#[subscription(name = "subscribe", item = String)]
async fn sub(&self) -> Result<(), Error>;
Expand Down
5 changes: 2 additions & 3 deletions proc-macros/tests/ui/incorrect/rpc/rpc_empty_bounds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use jsonrpsee::core::Error;
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

pub trait Config {
type Hash: Send + Sync + 'static;
Expand All @@ -10,7 +9,7 @@ pub trait Config {
#[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())]
pub trait EmptyBounds<Conf: Config> {
#[method(name = "bar")]
fn method(&self) -> Result<Conf::Hash, Error>;
fn method(&self) -> RpcResult<Conf::Hash>;
}

fn main() {}
66 changes: 33 additions & 33 deletions proc-macros/tests/ui/incorrect/rpc/rpc_empty_bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
error[E0277]: the trait bound `<Conf as Config>::Hash: Serialize` is not satisfied
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:10:1
|
10 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `<Conf as Config>::Hash`
|
= note: required for `std::result::Result<<Conf as Config>::Hash, jsonrpsee::jsonrpsee_core::Error>` to implement `IntoResponse`
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:9:1
|
9 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `<Conf as Config>::Hash`
|
= note: required for `std::result::Result<<Conf as Config>::Hash, ErrorObject<'_>>` to implement `IntoResponse`
note: required by a bound in `RpcModule::<Context>::register_method`
--> $WORKSPACE/core/src/server/rpc_module.rs
|
| R: IntoResponse + 'static,
| ^^^^^^^^^^^^ required by this bound in `RpcModule::<Context>::register_method`
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)
--> $WORKSPACE/core/src/server/rpc_module.rs
|
| R: IntoResponse + 'static,
| ^^^^^^^^^^^^ required by this bound in `RpcModule::<Context>::register_method`
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `<Conf as Config>::Hash: Clone` is not satisfied
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:10:1
|
10 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<Conf as Config>::Hash`
|
= note: required for `std::result::Result<<Conf as Config>::Hash, jsonrpsee::jsonrpsee_core::Error>` to implement `IntoResponse`
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:9:1
|
9 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<Conf as Config>::Hash`
|
= note: required for `std::result::Result<<Conf as Config>::Hash, ErrorObject<'_>>` to implement `IntoResponse`
note: required by a bound in `RpcModule::<Context>::register_method`
--> $WORKSPACE/core/src/server/rpc_module.rs
|
| R: IntoResponse + 'static,
| ^^^^^^^^^^^^ required by this bound in `RpcModule::<Context>::register_method`
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)
--> $WORKSPACE/core/src/server/rpc_module.rs
|
| R: IntoResponse + 'static,
| ^^^^^^^^^^^^ required by this bound in `RpcModule::<Context>::register_method`
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `for<'de> <Conf as Config>::Hash: Deserialize<'de>` is not satisfied
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:10:1
|
10 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'de> Deserialize<'de>` is not implemented for `<Conf as Config>::Hash`
|
= note: required for `<Conf as Config>::Hash` to implement `DeserializeOwned`
--> tests/ui/incorrect/rpc/rpc_empty_bounds.rs:9:1
|
9 | #[rpc(server, client, namespace = "foo", client_bounds(), server_bounds())]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'de> Deserialize<'de>` is not implemented for `<Conf as Config>::Hash`
|
= note: required for `<Conf as Config>::Hash` to implement `DeserializeOwned`
note: required by a bound in `request`
--> $WORKSPACE/core/src/client/mod.rs
|
| R: DeserializeOwned,
| ^^^^^^^^^^^^^^^^ required by this bound in `ClientT::request`
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)
--> $WORKSPACE/core/src/client/mod.rs
|
| R: DeserializeOwned,
| ^^^^^^^^^^^^^^^^ required by this bound in `ClientT::request`
= note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion server/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl std::error::Error for MyAppError {}

impl From<MyAppError> for ErrorObjectOwned {
fn from(_: MyAppError) -> Self {
ErrorObject::owned(1, "MyAppError", None::<()>)
ErrorObject::owned(-32000, "MyAppError", None::<()>)
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async fn single_method_call_with_faulty_context() {
let req = r#"{"jsonrpc":"2.0","method":"should_err","params":[],"id":1}"#;
let response = http_request(req.into(), uri).with_default_timeout().await.unwrap().unwrap();
assert_eq!(response.status, StatusCode::OK);
assert_eq!(response.body, call_execution_failed("RPC context failed", Id::Num(1)));
assert_eq!(response.body, call_execution_failed("MyAppError", Id::Num(1)));
}

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions server/src/tests/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ async fn single_method_call_with_faulty_context() {

let req = r#"{"jsonrpc":"2.0","method":"should_err", "params":[],"id":1}"#;
let response = client.send_request_text(req).with_default_timeout().await.unwrap().unwrap();
assert_eq!(response, call_execution_failed("RPC context failed", Id::Num(1)));
assert_eq!(response, call_execution_failed("MyAppError", Id::Num(1)));
}

#[tokio::test]
Expand Down Expand Up @@ -356,7 +356,7 @@ async fn async_method_call_that_fails() {

let req = r#"{"jsonrpc":"2.0","method":"err_async", "params":[],"id":1}"#;
let response = client.send_request_text(req).await.unwrap();
assert_eq!(response, call_execution_failed("nah", Id::Num(1)));
assert_eq!(response, call_execution_failed("MyAppError", Id::Num(1)));
}

#[tokio::test]
Expand Down
2 changes: 1 addition & 1 deletion types/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'a> Params<'a> {
{
// NOTE(niklasad1): Option::None is serialized as `null` so we provide that here.
let params = self.0.as_ref().map(AsRef::as_ref).unwrap_or("null");
serde_json::from_str(params).map_err(|e| invalid_params(e))
serde_json::from_str(params).map_err(invalid_params)
}

/// Attempt to parse parameters as an array of a single value of type `T`, and returns that value.
Expand Down

0 comments on commit 8f0a165

Please sign in to comment.