From c8e62624cf5a682b462e105bda06858cc815d121 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 7 Jul 2022 03:29:47 +0000 Subject: [PATCH] allow ids in errors --- web3-proxy/src/app.rs | 2 +- web3-proxy/src/connection.rs | 2 -- web3-proxy/src/frontend/errors.rs | 10 ++++++---- web3-proxy/src/frontend/http_proxy.rs | 4 +++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/web3-proxy/src/app.rs b/web3-proxy/src/app.rs index 19556718..f744604e 100644 --- a/web3-proxy/src/app.rs +++ b/web3-proxy/src/app.rs @@ -10,7 +10,7 @@ use futures::stream::StreamExt; use futures::Future; use linkedhashmap::LinkedHashMap; use parking_lot::RwLock; -use redis_cell_client::{bb8, RedisCellClient, RedisClientPool, RedisConnectionManager}; +use redis_cell_client::{bb8, RedisCellClient, RedisConnectionManager}; use serde_json::json; use std::fmt; use std::pin::Pin; diff --git a/web3-proxy/src/connection.rs b/web3-proxy/src/connection.rs index b143a1bb..86b020de 100644 --- a/web3-proxy/src/connection.rs +++ b/web3-proxy/src/connection.rs @@ -444,8 +444,6 @@ impl Web3Connection { let mut interval = interval(Duration::from_secs(60)); interval.set_missed_tick_behavior(MissedTickBehavior::Delay); - // TODO: create a filter - loop { // TODO: actually do something here /* diff --git a/web3-proxy/src/frontend/errors.rs b/web3-proxy/src/frontend/errors.rs index 24e16f30..606ac1f1 100644 --- a/web3-proxy/src/frontend/errors.rs +++ b/web3-proxy/src/frontend/errors.rs @@ -1,13 +1,12 @@ use axum::{http::StatusCode, response::IntoResponse, Json}; use serde_json::value::RawValue; -use tracing::warn; use crate::jsonrpc::JsonRpcForwardedResponse; pub async fn handler_404() -> impl IntoResponse { let err = anyhow::anyhow!("nothing to see here"); - handle_anyhow_error(Some(StatusCode::NOT_FOUND), err).await + handle_anyhow_error(Some(StatusCode::NOT_FOUND), None, err).await } /// handle errors by converting them into something that implements `IntoResponse` @@ -15,13 +14,16 @@ pub async fn handler_404() -> impl IntoResponse { /// TODO: i think we want a custom result type instead. put the anyhow result inside. then `impl IntoResponse for CustomResult` pub async fn handle_anyhow_error( code: Option, + id: Option>, err: anyhow::Error, ) -> impl IntoResponse { - let id = RawValue::from_string("null".to_string()).unwrap(); + // TODO: we might have an id. like if this is for rate limiting, we can use it + let id = id.unwrap_or_else(|| RawValue::from_string("null".to_string()).unwrap()); let err = JsonRpcForwardedResponse::from_anyhow_error(err, id); - warn!("Responding with error: {:?}", err); + // TODO: logs here are too verbose. emit a stat + // warn!("Responding with error: {:?}", err); let code = code.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR); diff --git a/web3-proxy/src/frontend/http_proxy.rs b/web3-proxy/src/frontend/http_proxy.rs index 0543431f..d859270f 100644 --- a/web3-proxy/src/frontend/http_proxy.rs +++ b/web3-proxy/src/frontend/http_proxy.rs @@ -16,8 +16,10 @@ pub async fn proxy_web3_rpc( if rate_limiter.throttle_key(&rate_limiter_key).await.is_err() { // TODO: set headers so they know when they can retry // warn!(?ip, "public rate limit exceeded"); + // TODO: use their id if possible return handle_anyhow_error( Some(StatusCode::TOO_MANY_REQUESTS), + None, anyhow::anyhow!("too many requests"), ) .await @@ -29,6 +31,6 @@ pub async fn proxy_web3_rpc( match app.proxy_web3_rpc(payload).await { Ok(response) => (StatusCode::OK, Json(&response)).into_response(), - Err(err) => handle_anyhow_error(None, err).await.into_response(), + Err(err) => handle_anyhow_error(None, None, err).await.into_response(), } }