From 2e830c315489aca0da0f3be1d90413307fd409dc Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sat, 7 May 2022 01:07:38 +0000 Subject: [PATCH] fix errors --- web3-proxy/src/connection.rs | 13 +++++++++- web3-proxy/src/connections.rs | 4 +-- web3-proxy/src/main.rs | 48 +++++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/web3-proxy/src/connection.rs b/web3-proxy/src/connection.rs index 3c232bc8..5e34c927 100644 --- a/web3-proxy/src/connection.rs +++ b/web3-proxy/src/connection.rs @@ -357,6 +357,17 @@ impl fmt::Debug for JsonRpcRequest { } } +#[derive(Serialize, Clone)] +pub struct JsonRpcErrorData { + /// The error code + pub code: i64, + /// The error message + pub message: String, + /// Additional data + #[serde(skip_serializing_if = "Option::is_none")] + pub data: Option, +} + #[derive(Clone, Serialize)] pub struct JsonRpcForwardedResponse { pub jsonrpc: String, @@ -364,7 +375,7 @@ pub struct JsonRpcForwardedResponse { #[serde(skip_serializing_if = "Option::is_none")] pub result: Option>, #[serde(skip_serializing_if = "Option::is_none")] - pub error: Option, + pub error: Option, // TODO: optional error } diff --git a/web3-proxy/src/connections.rs b/web3-proxy/src/connections.rs index 6128b2fc..40674ff5 100644 --- a/web3-proxy/src/connections.rs +++ b/web3-proxy/src/connections.rs @@ -112,14 +112,14 @@ impl Web3Connections { connection_handle: ActiveRequestHandle, method: &str, params: &RawValue, - ) -> anyhow::Result> { + ) -> Result, ethers::prelude::ProviderError> { // connection.in_active_requests was called when this rpc was selected let response = connection_handle.request(method, params).await; // TODO: if "no block with that header" or some other jsonrpc errors, skip this response - response.map_err(Into::into) + response } pub async fn try_send_requests( diff --git a/web3-proxy/src/main.rs b/web3-proxy/src/main.rs index 31e9f3a4..ef590441 100644 --- a/web3-proxy/src/main.rs +++ b/web3-proxy/src/main.rs @@ -3,7 +3,12 @@ mod connection; mod connections; use config::Web3ConnectionConfig; +use connection::JsonRpcErrorData; use connection::JsonRpcForwardedResponse; +use ethers::prelude::HttpClientError; +use ethers::prelude::ProviderError; +// use ethers::providers::transports::common::JsonRpcError; + use futures::future; use governor::clock::{Clock, QuantaClock}; use linkedhashmap::LinkedHashMap; @@ -246,12 +251,51 @@ impl Web3ProxyApp { response } Err(e) => { - // TODO: what is the proper format for an error? + let code; + let message: String; + let data; + + match e { + ProviderError::JsonRpcClientError(e) => { + // TODO: better log. get the original code somehow. its buried deep inside boxes though + // warn!("JsonRpcClientError: {:?}", e); + + // TODO: we should check what type the provider is rather than trying to downcast both types of errors + let e: Result, _> = e.downcast(); + if let Ok(e) = &e { + match &**e { + HttpClientError::JsonRpcError(e) => { + code = e.code; + message = e.message.clone(); + data = e.data.clone(); + } + e => { + // TODO: improve this + code = -32603; + message = format!("{}", e); + data = None; + } + } + } else { + unimplemented!(); + } + } + _ => { + code = -32603; + message = format!("{}", e); + data = None; + } + } + JsonRpcForwardedResponse { jsonrpc: "2.0".to_string(), id: json_body.id, result: None, - error: Some(format!("{}", e)), + error: Some(JsonRpcErrorData { + code, + message, + data, + }), } } };