fix errors

This commit is contained in:
Bryan Stitt 2022-05-07 01:07:38 +00:00
parent 21761435f2
commit 2e830c3154
3 changed files with 60 additions and 5 deletions

View File

@ -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<serde_json::Value>,
}
#[derive(Clone, Serialize)] #[derive(Clone, Serialize)]
pub struct JsonRpcForwardedResponse { pub struct JsonRpcForwardedResponse {
pub jsonrpc: String, pub jsonrpc: String,
@ -364,7 +375,7 @@ pub struct JsonRpcForwardedResponse {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Box<RawValue>>, pub result: Option<Box<RawValue>>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>, pub error: Option<JsonRpcErrorData>,
// TODO: optional error // TODO: optional error
} }

View File

@ -112,14 +112,14 @@ impl Web3Connections {
connection_handle: ActiveRequestHandle, connection_handle: ActiveRequestHandle,
method: &str, method: &str,
params: &RawValue, params: &RawValue,
) -> anyhow::Result<Box<RawValue>> { ) -> Result<Box<RawValue>, ethers::prelude::ProviderError> {
// connection.in_active_requests was called when this rpc was selected // connection.in_active_requests was called when this rpc was selected
let response = connection_handle.request(method, params).await; let response = connection_handle.request(method, params).await;
// TODO: if "no block with that header" or some other jsonrpc errors, skip this response // 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( pub async fn try_send_requests(

View File

@ -3,7 +3,12 @@ mod connection;
mod connections; mod connections;
use config::Web3ConnectionConfig; use config::Web3ConnectionConfig;
use connection::JsonRpcErrorData;
use connection::JsonRpcForwardedResponse; use connection::JsonRpcForwardedResponse;
use ethers::prelude::HttpClientError;
use ethers::prelude::ProviderError;
// use ethers::providers::transports::common::JsonRpcError;
use futures::future; use futures::future;
use governor::clock::{Clock, QuantaClock}; use governor::clock::{Clock, QuantaClock};
use linkedhashmap::LinkedHashMap; use linkedhashmap::LinkedHashMap;
@ -246,12 +251,51 @@ impl Web3ProxyApp {
response response
} }
Err(e) => { 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<Box<HttpClientError>, _> = 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 { JsonRpcForwardedResponse {
jsonrpc: "2.0".to_string(), jsonrpc: "2.0".to_string(),
id: json_body.id, id: json_body.id,
result: None, result: None,
error: Some(format!("{}", e)), error: Some(JsonRpcErrorData {
code,
message,
data,
}),
} }
} }
}; };