error: RpcError with custom client error (#694)

* error: `RpcError` with custom client error

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* error: Add `SubscriptionDropped` and panic on param errors

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update subxt/src/rpc/rpc_client_t.rs

Co-authored-by: James Wilson <james@jsdw.me>

* Apply rustfmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
Alexandru Vasile
2022-10-20 16:33:50 +03:00
committed by GitHub
parent 1736f618d9
commit 6742fe0b12
5 changed files with 29 additions and 21 deletions
+11 -15
View File
@@ -32,10 +32,10 @@ impl RpcClientT for Client {
params: Option<Box<RawValue>>,
) -> RpcFuture<'a, Box<RawValue>> {
Box::pin(async move {
let params = prep_params_for_jsonrpsee(params)?;
let params = prep_params_for_jsonrpsee(params);
let res = ClientT::request(self, method, Some(params))
.await
.map_err(|e| RpcError(e.to_string()))?;
.map_err(|e| RpcError::ClientError(Box::new(e)))?;
Ok(res)
})
}
@@ -47,7 +47,7 @@ impl RpcClientT for Client {
unsub: &'a str,
) -> RpcFuture<'a, RpcSubscription> {
Box::pin(async move {
let params = prep_params_for_jsonrpsee(params)?;
let params = prep_params_for_jsonrpsee(params);
let sub = SubscriptionClientT::subscribe::<Box<RawValue>>(
self,
sub,
@@ -55,8 +55,8 @@ impl RpcClientT for Client {
unsub,
)
.await
.map_err(|e| RpcError(e.to_string()))?
.map_err(|e| RpcError(e.to_string()))
.map_err(|e| RpcError::ClientError(Box::new(e)))?
.map_err(|e| RpcError::ClientError(Box::new(e)))
.boxed();
Ok(sub)
})
@@ -65,22 +65,18 @@ impl RpcClientT for Client {
// This is ugly; we have to encode to Value's to be compat with the jsonrpc interface.
// Remove and simplify this once something like https://github.com/paritytech/jsonrpsee/issues/862 is in:
fn prep_params_for_jsonrpsee(
params: Option<Box<RawValue>>,
) -> Result<ParamsSer<'static>, RpcError> {
fn prep_params_for_jsonrpsee(params: Option<Box<RawValue>>) -> ParamsSer<'static> {
let params = match params {
Some(params) => params,
// No params? avoid any work and bail early.
None => return Ok(ParamsSer::Array(Vec::new())),
None => return ParamsSer::Array(Vec::new()),
};
let val = serde_json::to_value(&params).expect("RawValue guarantees valid JSON");
let arr = match val {
Value::Array(arr) => Ok(arr),
Value::Array(arr) => arr,
_ => {
Err(RpcError(format!(
"RPC Params are expected to be an array but got {params}"
)))
panic!("RPC Params are expected to be an array but got {params}");
}
}?;
Ok(ParamsSer::Array(arr))
};
ParamsSer::Array(arr)
}
+5
View File
@@ -20,6 +20,11 @@ pub use serde_json::value::RawValue;
/// the caller. This is the case because we want the methods to be object-safe (which prohibits
/// generics), and want to avoid any unnecessary allocations in serializing/deserializing
/// parameters.
///
/// # Panics
///
/// Implementations are free to panic if the `RawValue`'s passed to `request_raw` or
/// `subscribe_raw` are not JSON arrays. Internally, we ensure that this is always the case.
pub trait RpcClientT: Send + Sync + 'static {
/// Make a raw request for which we expect a single response back from. Implementations
/// should expect that the params will either be `None`, or be an already-serialized