async_json_rpc/clients/
mod.rs

1pub mod http;
2
3use std::{error, fmt};
4
5pub trait RequestFactory {
6    fn build_request(&self) -> crate::objects::RequestBuilder;
7}
8
9/// The error type for RPCs.
10#[derive(Debug)]
11pub enum Error<E> {
12    /// The batch response contained a duplicate ID.
13    BatchDuplicateResponseId(serde_json::Value),
14    /// A connection error occured.
15    Connection(E),
16    /// Batches can't be empty.
17    EmptyBatch,
18    /// An error occured during respnse JSON deserialization.
19    Json(serde_json::Error),
20    /// The response did not have the expected nonce.
21    NonceMismatch,
22    /// The response had a jsonrpc field other than "2.0".
23    VersionMismatch,
24    /// The batch response contained an ID that didn't correspond to any request ID.
25    WrongBatchResponseId(serde_json::Value),
26    /// Too many responses returned in batch.
27    WrongBatchResponseSize,
28}
29
30impl<E: fmt::Display> fmt::Display for Error<E> {
31    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32        let printable = match self {
33            Error::BatchDuplicateResponseId(err) => {
34                return write!(f, "duplicate batch response id, {}", err)
35            }
36            Error::Connection(err) => return err.fmt(f),
37            Error::EmptyBatch => "empty batch",
38            Error::Json(err) => return err.fmt(f),
39            Error::NonceMismatch => "nonce mismatch",
40            Error::VersionMismatch => "version mismatch",
41            Error::WrongBatchResponseId(err) => {
42                return write!(f, "wrong batch response id, {}", err)
43            }
44            Error::WrongBatchResponseSize => "wrong batch response size",
45        };
46        write!(f, "{}", printable)
47    }
48}
49
50impl<E: fmt::Display + fmt::Debug> error::Error for Error<E> {}