feat(rpc): add default to_rpc_result function (#993)

This commit is contained in:
Matthias Seitz
2023-01-23 22:13:13 +01:00
committed by GitHub
parent 68c76664d0
commit 865367fe46
2 changed files with 11 additions and 1 deletions

View File

@ -55,7 +55,7 @@ impl AdminApiServer for AdminApi {
async fn node_info(&self) -> RpcResult<NodeInfo> { async fn node_info(&self) -> RpcResult<NodeInfo> {
let enr = self.network.local_node_record(); let enr = self.network.local_node_record();
let status = self.network.network_status().await.map_internal_err(|e| e.to_string())?; let status = self.network.network_status().await.to_rpc_result()?;
Ok(NodeInfo::new(enr, status)) Ok(NodeInfo::new(enr, status))
} }

View File

@ -1,9 +1,19 @@
//! Additional helpers for converting errors. //! Additional helpers for converting errors.
use jsonrpsee::core::{Error as RpcError, RpcResult}; use jsonrpsee::core::{Error as RpcError, RpcResult};
use std::fmt::Display;
/// Helper trait to easily convert various `Result` types into [`RpcResult`] /// Helper trait to easily convert various `Result` types into [`RpcResult`]
pub(crate) trait ToRpcResult<Ok, Err> { pub(crate) trait ToRpcResult<Ok, Err> {
/// Converts the error of the [Result] to an [RpcResult] via the `Err` [Display] impl.
fn to_rpc_result(self) -> RpcResult<Ok>
where
Err: Display,
Self: Sized,
{
self.map_internal_err(|err| err.to_string())
}
/// Converts this type into an [`RpcResult`] /// Converts this type into an [`RpcResult`]
fn map_rpc_err<'a, F, M>(self, op: F) -> RpcResult<Ok> fn map_rpc_err<'a, F, M>(self, op: F) -> RpcResult<Ok>
where where