From d8b1ca779b65cb27bc9d9de4964d8dc5dbafcb05 Mon Sep 17 00:00:00 2001 From: Tadas Date: Fri, 2 Jun 2023 15:28:56 +0300 Subject: [PATCH] Handle json-rpc failure object as root element on batched requests --- src/transports/http.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/transports/http.rs b/src/transports/http.rs index 096a26ce..2d0666bc 100644 --- a/src/transports/http.rs +++ b/src/transports/http.rs @@ -137,12 +137,28 @@ impl BatchTransport for Http { let (client, url) = self.new_request(); let (ids, calls): (Vec<_>, Vec<_>) = requests.into_iter().unzip(); Box::pin(async move { - let outputs: Vec = execute_rpc(&client, url, &Request::Batch(calls), id).await?; + let value = execute_rpc(&client, url, &Request::Batch(calls), id).await?; + let outputs = handle_possible_error_object_for_batched_request(value)?; handle_batch_response(&ids, outputs) }) } } +fn handle_possible_error_object_for_batched_request(value: Value) -> Result> { + if value.is_object() { + let output: Output = serde_json::from_value(value)?; + return Err(match output { + Output::Failure(failure) => Error::Rpc(failure.error), + Output::Success(success) => { + // totally unlikely - we got json success object for batched request + Error::InvalidResponse(format!("Invalid response for batched request: {:?}", success)) + } + }); + } + let outputs = serde_json::from_value(value)?; + Ok(outputs) +} + // According to the jsonrpc specification batch responses can be returned in any order so we need to // restore the intended order. fn handle_batch_response(ids: &[RequestId], outputs: Vec) -> Result> {