Unify error enums in substrate and ethereum clients with thiserror (#1094)

* Unify error enums in substrate and ethereum clients with `thiserror`

Related to https://github.com/paritytech/parity-bridges-common/issues/857

* Add license pre-amble

* rustfmt

* Fix spelling
This commit is contained in:
Vladislav
2021-10-22 13:15:50 +03:00
committed by Bastian Köcher
parent 7b4f1c2236
commit 5842968273
48 changed files with 482 additions and 381 deletions
@@ -14,8 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
use crate::metrics::{
metric_name, register, F64SharedRef, Gauge, PrometheusError, Registry, StandaloneMetrics, F64,
use crate::{
error::{self, Error},
metrics::{
metric_name, register, F64SharedRef, Gauge, PrometheusError, Registry, StandaloneMetrics,
F64,
},
};
use async_std::sync::{Arc, RwLock};
@@ -61,27 +65,18 @@ impl FloatJsonValueMetric {
self.shared_value_ref.clone()
}
/// Read value from HTTP service.
async fn read_value(&self) -> Result<f64, String> {
/// Request value from HTTP service.
async fn request_value(&self) -> anyhow::Result<String> {
use isahc::{AsyncReadResponseExt, HttpClient, Request};
fn map_isahc_err(err: impl std::fmt::Display) -> String {
format!("Failed to fetch token price from remote server: {}", err)
}
let request = Request::get(&self.url)
.header("Accept", "application/json")
.body(())
.map_err(map_isahc_err)?;
let raw_response = HttpClient::new()
.map_err(map_isahc_err)?
.send_async(request)
.await
.map_err(map_isahc_err)?
.text()
.await
.map_err(map_isahc_err)?;
let request = Request::get(&self.url).header("Accept", "application/json").body(())?;
let raw_response = HttpClient::new()?.send_async(request).await?.text().await?;
Ok(raw_response)
}
/// Read value from HTTP service.
async fn read_value(&self) -> error::Result<f64> {
let raw_response = self.request_value().await.map_err(Error::FetchTokenPrice)?;
parse_service_response(&self.json_path, &raw_response)
}
}
@@ -94,30 +89,26 @@ impl StandaloneMetrics for FloatJsonValueMetric {
async fn update(&self) {
let value = self.read_value().await;
crate::metrics::set_gauge_value(&self.metric, value.clone().map(Some));
*self.shared_value_ref.write().await = value.ok();
let maybe_ok = value.as_ref().ok().copied();
crate::metrics::set_gauge_value(&self.metric, value.map(Some));
*self.shared_value_ref.write().await = maybe_ok;
}
}
/// Parse HTTP service response.
fn parse_service_response(json_path: &str, response: &str) -> Result<f64, String> {
let json = serde_json::from_str(response).map_err(|err| {
format!("Failed to parse HTTP service response: {:?}. Response: {:?}", err, response,)
})?;
fn parse_service_response(json_path: &str, response: &str) -> error::Result<f64> {
let json =
serde_json::from_str(response).map_err(|err| Error::ParseHttp(err, response.to_owned()))?;
let mut selector = jsonpath_lib::selector(&json);
let maybe_selected_value = selector(json_path).map_err(|err| {
format!("Failed to select value from response: {:?}. Response: {:?}", err, response,)
})?;
let maybe_selected_value =
selector(json_path).map_err(|err| Error::SelectResponseValue(err, response.to_owned()))?;
let selected_value = maybe_selected_value
.first()
.and_then(|v| v.as_f64())
.ok_or_else(|| format!("Missing required value from response: {:?}", response,))?;
.ok_or_else(|| Error::MissingResponseValue(response.to_owned()))?;
if !selected_value.is_normal() || selected_value < 0.0 {
return Err(format!(
"Failed to parse float value {:?} from response. It is assumed to be positive and normal",
selected_value,
))
return Err(Error::ParseFloat(selected_value))
}
Ok(selected_value)