Unverified Commit 94795309 authored by Niklas Adolfsson's avatar Niklas Adolfsson Committed by GitHub
Browse files

fix(http client): more user-friendly error messages when decoding fails (#853)

* fix(http client): serde_json::Value -> T

This PR changes the http client to decode the reponse as `Response<serde_json::Value> -> Response<R>`
In order to get a more user-friendly error message

Before:

```
response: Err(ParseError(Error("missing field `error`", line: 1, column: 3752798)))
```

After

```
2022-08-11T13:57:12.033043Z  INFO http: r: Parse error: invalid type: string "lo", expected u8
```

* address grumbles

* Update client/http-client/src/client.rs

* Update client/http-client/src/client.rs
parent bf73876d
Pipeline #215075 passed with stages
in 4 minutes and 31 seconds
......@@ -35,7 +35,7 @@ use jsonrpsee_core::client::{CertificateStore, ClientT, IdKind, RequestIdManager
use jsonrpsee_core::params::BatchRequestBuilder;
use jsonrpsee_core::tracing::RpcTracing;
use jsonrpsee_core::traits::ToRpcParams;
use jsonrpsee_core::{Error, TEN_MB_SIZE_BYTES};
use jsonrpsee_core::{Error, JsonRawValue, TEN_MB_SIZE_BYTES};
use jsonrpsee_types::error::CallError;
use rustc_hash::FxHashMap;
use serde::de::DeserializeOwned;
......@@ -216,7 +216,9 @@ impl ClientT for HttpClient {
}
};
let response: Response<_> = match serde_json::from_slice(&body) {
// 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: Response<&JsonRawValue> = match serde_json::from_slice(&body) {
Ok(response) => response,
Err(_) => {
let err: ErrorResponse = serde_json::from_slice(&body).map_err(Error::ParseError)?;
......@@ -224,8 +226,10 @@ impl ClientT for HttpClient {
}
};
let result = serde_json::from_str(response.result.get()).map_err(Error::ParseError)?;
if response.id == id {
Ok(response.result)
Ok(result)
} else {
Err(Error::InvalidRequestId)
}
......@@ -264,7 +268,9 @@ impl ClientT for HttpClient {
Ok(Err(e)) => return Err(Error::Transport(e.into())),
};
let rps: Vec<Response<_>> =
// 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 rps: Vec<Response<&JsonRawValue>> =
serde_json::from_slice(&body).map_err(|_| match serde_json::from_slice::<ErrorResponse>(&body) {
Ok(e) => Error::Call(CallError::Custom(e.error_object().clone().into_owned())),
Err(e) => Error::ParseError(e),
......@@ -277,7 +283,8 @@ impl ClientT for HttpClient {
Some(pos) => *pos,
None => return Err(Error::InvalidRequestId),
};
responses[pos] = rp.result
let result = serde_json::from_str(rp.result.get()).map_err(Error::ParseError)?;
responses[pos] = result;
}
Ok(responses)
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment