From 865367fe4643fd3a9eb28f6a9b145063756d36ee Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 23 Jan 2023 22:13:13 +0100 Subject: [PATCH] feat(rpc): add default to_rpc_result function (#993) --- crates/net/rpc/src/admin.rs | 2 +- crates/net/rpc/src/result.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/net/rpc/src/admin.rs b/crates/net/rpc/src/admin.rs index 830007fd6..6ecb0aaf1 100644 --- a/crates/net/rpc/src/admin.rs +++ b/crates/net/rpc/src/admin.rs @@ -55,7 +55,7 @@ impl AdminApiServer for AdminApi { async fn node_info(&self) -> RpcResult { 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)) } diff --git a/crates/net/rpc/src/result.rs b/crates/net/rpc/src/result.rs index 2ca209196..f8b9087af 100644 --- a/crates/net/rpc/src/result.rs +++ b/crates/net/rpc/src/result.rs @@ -1,9 +1,19 @@ //! Additional helpers for converting errors. use jsonrpsee::core::{Error as RpcError, RpcResult}; +use std::fmt::Display; /// Helper trait to easily convert various `Result` types into [`RpcResult`] pub(crate) trait ToRpcResult { + /// Converts the error of the [Result] to an [RpcResult] via the `Err` [Display] impl. + fn to_rpc_result(self) -> RpcResult + where + Err: Display, + Self: Sized, + { + self.map_internal_err(|err| err.to_string()) + } + /// Converts this type into an [`RpcResult`] fn map_rpc_err<'a, F, M>(self, op: F) -> RpcResult where