Skip to content

Commit

Permalink
Handle json-rpc failure object as root element on batched requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tadovas committed Jun 2, 2023
1 parent b5f728e commit 319d972
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/transports/http.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! HTTP Transport
use crate::Error::{InvalidResponse, Rpc};
use crate::{
error::{Error, Result, TransportError},
helpers, BatchTransport, RequestId, Transport,
Expand Down Expand Up @@ -137,12 +138,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<Output> = 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<Vec<Output>> {
if value.is_object() {
let output: Output = serde_json::from_value(value)?;
return Err(match output {
Output::Failure(failure) => Rpc(failure.error),
Output::Success(success) => {
// totally unlikely - we got json success object for batched request
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<Output>) -> Result<Vec<RpcResult>> {
Expand Down

0 comments on commit 319d972

Please sign in to comment.