mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
feat: add Error extension trait for Result<Option<Block>> (#1381)
Co-authored-by: Georgios Konstantopoulos <me@gakonst.com> Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
committed by
GitHub
parent
39949fdd08
commit
9dae54a3ed
@ -1,8 +1,12 @@
|
||||
//! Additional helpers for converting errors.
|
||||
|
||||
use jsonrpsee::core::{Error as RpcError, RpcResult};
|
||||
use reth_interfaces::Result as RethResult;
|
||||
use reth_primitives::Block;
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::eth::error::EthApiError;
|
||||
|
||||
/// Helper trait to easily convert various `Result` types into [`RpcResult`]
|
||||
pub(crate) trait ToRpcResult<Ok, Err> {
|
||||
/// Converts the error of the [Result] to an [RpcResult] via the `Err` [Display] impl.
|
||||
@ -104,6 +108,30 @@ macro_rules! impl_to_rpc_result {
|
||||
impl_to_rpc_result!(reth_interfaces::Error);
|
||||
impl_to_rpc_result!(reth_network_api::NetworkError);
|
||||
|
||||
/// An extension to used to apply error conversions to various result types
|
||||
pub(crate) trait ToRpcResultExt {
|
||||
/// The `Ok` variant of the [RpcResult]
|
||||
type Ok;
|
||||
|
||||
/// Maps the `Ok` variant of this type into [Self::Ok] and maps the `Err` variant into rpc
|
||||
/// error.
|
||||
fn map_ok_or_rpc_err(self) -> RpcResult<<Self as ToRpcResultExt>::Ok>;
|
||||
}
|
||||
|
||||
impl ToRpcResultExt for RethResult<Option<Block>> {
|
||||
type Ok = Block;
|
||||
|
||||
fn map_ok_or_rpc_err(self) -> RpcResult<<Self as ToRpcResultExt>::Ok> {
|
||||
match self {
|
||||
Ok(block) => match block {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(EthApiError::UnknownBlockNumber.into()),
|
||||
},
|
||||
Err(err) => Err(internal_rpc_err(err.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs an internal JSON-RPC error.
|
||||
pub(crate) fn internal_rpc_err(msg: impl Into<String>) -> jsonrpsee::core::Error {
|
||||
rpc_err(jsonrpsee::types::error::INTERNAL_ERROR_CODE, msg, None)
|
||||
@ -133,6 +161,7 @@ pub(crate) fn rpc_err(code: i32, msg: impl Into<String>, data: Option<&[u8]>) ->
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
fn assert_rpc_result<Ok, Err, T: ToRpcResult<Ok, Err>>() {}
|
||||
@ -141,6 +170,10 @@ mod tests {
|
||||
Ok(o)
|
||||
}
|
||||
|
||||
fn to_optional_reth_err<Ok>(o: Ok) -> reth_interfaces::Result<Option<Ok>> {
|
||||
Ok(Some(o))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_convert_rpc() {
|
||||
assert_rpc_result::<(), reth_interfaces::Error, reth_interfaces::Result<()>>();
|
||||
|
||||
Reference in New Issue
Block a user