From 8e499a17353151844b366453c0d99f22b2456c08 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 12 May 2022 21:53:24 +0000 Subject: [PATCH] almost correct errors --- web3-proxy/src/app.rs | 1 + web3-proxy/src/main.rs | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/web3-proxy/src/app.rs b/web3-proxy/src/app.rs index 574dd550..700079e2 100644 --- a/web3-proxy/src/app.rs +++ b/web3-proxy/src/app.rs @@ -308,6 +308,7 @@ impl Web3ProxyApp { response } Err(e) => { + // TODO: move this to a helper function? let code; let message: String; let data; diff --git a/web3-proxy/src/main.rs b/web3-proxy/src/main.rs index 945fc183..9b8b6ced 100644 --- a/web3-proxy/src/main.rs +++ b/web3-proxy/src/main.rs @@ -4,6 +4,8 @@ mod connection; mod connections; mod jsonrpc; +use jsonrpc::{JsonRpcErrorData, JsonRpcForwardedResponse}; +use serde_json::value::RawValue; use std::fs; use std::sync::atomic::{self, AtomicUsize}; use std::sync::Arc; @@ -72,11 +74,24 @@ fn handle_anyhow_errors( ) -> warp::http::Response { match res { Ok(r) => r.into_response(), - Err(e) => warp::reply::with_status( - // TODO: json error - format!("{}", e), - warp::http::StatusCode::INTERNAL_SERVER_ERROR, - ) + Err(e) => { + let e = JsonRpcForwardedResponse { + jsonrpc: "2.0".to_string(), + // TODO: what id can we use? how do we make sure it gets attached to this? + id: RawValue::from_string("0".to_string()).unwrap(), + result: None, + error: Some(JsonRpcErrorData { + code: -32099, + message: format!("{:?}", e), + data: None, + }), + }; + + warp::reply::with_status( + serde_json::to_string(&e).unwrap(), + warp::http::StatusCode::INTERNAL_SERVER_ERROR, + ) + } .into_response(), } }