less json errors

This commit is contained in:
Bryan Stitt 2022-06-03 21:45:44 +00:00
parent d2033b0a4e
commit 24b76a33bc
2 changed files with 56 additions and 42 deletions

View File

@ -469,17 +469,32 @@ impl Web3Connections {
.request(&request.method, &request.params) .request(&request.method, &request.params)
.await; .await;
let response = match JsonRpcForwardedResponse::from_response_result(
JsonRpcForwardedResponse::from_response_result(response_result, request.id); response_result,
request.id.clone(),
) {
Ok(response) => {
if response.error.is_some() {
trace!(?response, "Sending error reply",);
// errors already sent false to the in_flight_tx
} else {
trace!(?response, "Sending reply");
}
if response.error.is_some() { return Ok(response);
trace!(?response, "Sending error reply",); }
// errors already sent false to the in_flight_tx Err(e) => {
} else { warn!(?self, ?e, "Backend server error!");
trace!(?response, "Sending reply");
// TODO: sleep how long? until synced_connections changes or rate limits are available
sleep(Duration::from_millis(100)).await;
// TODO: when we retry, depending on the error, skip using this same server
// for example, if rpc isn't available on this server, retrying is bad
// but if an http error, retrying on same is probably fine
continue;
}
} }
return Ok(response);
} }
Err(None) => { Err(None) => {
warn!(?self, "No servers in sync!"); warn!(?self, "No servers in sync!");

View File

@ -184,9 +184,9 @@ impl JsonRpcForwardedResponse {
pub fn from_response_result( pub fn from_response_result(
result: Result<Box<RawValue>, ProviderError>, result: Result<Box<RawValue>, ProviderError>,
id: Box<RawValue>, id: Box<RawValue>,
) -> Self { ) -> anyhow::Result<Self> {
match result { match result {
Ok(response) => Self::from_response(response, id), Ok(response) => Ok(Self::from_response(response, id)),
Err(e) => Self::from_ethers_error(e, id), Err(e) => Self::from_ethers_error(e, id),
} }
} }
@ -208,7 +208,7 @@ impl JsonRpcForwardedResponse {
} }
} }
pub fn from_ethers_error(e: ProviderError, id: Box<RawValue>) -> Self { pub fn from_ethers_error(e: ProviderError, id: Box<RawValue>) -> anyhow::Result<Self> {
// TODO: move turning ClientError into json to a helper function? // TODO: move turning ClientError into json to a helper function?
let code; let code;
let message: String; let message: String;
@ -216,47 +216,46 @@ impl JsonRpcForwardedResponse {
match e { match e {
ProviderError::JsonRpcClientError(e) => { ProviderError::JsonRpcClientError(e) => {
// TODO: we should check what type the provider is rather than trying to downcast both types of errors // TODO: check what type the provider is rather than trying to downcast both types of errors
if let Some(e) = e.downcast_ref::<HttpClientError>() { let e = e.downcast::<HttpClientError>();
match &*e {
if let Ok(e) = e {
match *e {
HttpClientError::JsonRpcError(e) => { HttpClientError::JsonRpcError(e) => {
code = e.code; code = e.code;
message = e.message.clone(); message = e.message.clone();
data = e.data.clone(); data = e.data;
} }
e => { e => {
// TODO: improve this // this is not an rpc error. keep it as an error
code = -32603; return Err(e.into());
message = format!("{}", e);
data = None;
}
}
} else if let Some(e) = e.downcast_ref::<WsClientError>() {
match &*e {
WsClientError::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 { } else {
unimplemented!(); // it wasn't an HttpClientError. try WsClientError
let e = e.unwrap_err().downcast::<WsClientError>();
if let Ok(e) = e {
match *e {
WsClientError::JsonRpcError(e) => {
code = e.code;
message = e.message.clone();
data = e.data;
}
e => {
// this is not an rpc error. keep it as an error
return Err(e.into());
}
}
} else {
unimplemented!();
}
} }
} }
_ => { e => return Err(e.into()),
code = -32603;
message = format!("{}", e);
data = None;
}
} }
Self { Ok(Self {
jsonrpc: "2.0".to_string(), jsonrpc: "2.0".to_string(),
id, id,
result: None, result: None,
@ -265,7 +264,7 @@ impl JsonRpcForwardedResponse {
message, message,
data, data,
}), }),
} })
} }
} }