allow ids in errors

This commit is contained in:
Bryan Stitt 2022-07-07 03:29:47 +00:00
parent 600df2cd3a
commit c8e62624cf
4 changed files with 10 additions and 8 deletions

View File

@ -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;

View File

@ -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
/*

View File

@ -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<StatusCode>,
id: Option<Box<RawValue>>,
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);

View File

@ -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(),
}
}