jsonrpc instead of 500 errors

This commit is contained in:
Bryan Stitt 2023-01-24 08:07:10 -08:00
parent c9b8e5dfb9
commit 447cf90eed
2 changed files with 61 additions and 13 deletions

@ -1098,9 +1098,15 @@ impl Web3ProxyApp {
| "shh_uninstallFilter" | "shh_uninstallFilter"
| "shh_version") => { | "shh_version") => {
// TODO: client error stat // TODO: client error stat
// TODO: proper error code // TODO: what error code?
// TODO: right now this sends a warn level log. thats too verbose return Ok((
return Err(anyhow::anyhow!("method unsupported: {}", method)); JsonRpcForwardedResponse::from_string(
format!("method unsupported: {}", method),
None,
Some(request_id),
),
vec![],
));
} }
// TODO: implement these commands // TODO: implement these commands
method @ ("eth_getFilterChanges" method @ ("eth_getFilterChanges"
@ -1110,8 +1116,15 @@ impl Web3ProxyApp {
| "eth_newPendingTransactionFilter" | "eth_newPendingTransactionFilter"
| "eth_uninstallFilter") => { | "eth_uninstallFilter") => {
// TODO: unsupported command stat // TODO: unsupported command stat
// TODO: right now this sends a warn level log. thats too verbose // TODO: what error code?
return Err(anyhow::anyhow!("not yet implemented: {}", method)); return Ok((
JsonRpcForwardedResponse::from_string(
format!("not yet implemented: {}", method),
None,
Some(request_id),
),
vec![],
));
} }
// some commands can use local data or caches // some commands can use local data or caches
"eth_accounts" => { "eth_accounts" => {
@ -1312,13 +1325,23 @@ impl Web3ProxyApp {
json!(false) json!(false)
} }
"eth_subscribe" => { "eth_subscribe" => {
return Err(anyhow::anyhow!( return Ok((
"notifications not supported. eth_subscribe is only available over a websocket" JsonRpcForwardedResponse::from_string(
format!("notifications not supported. eth_subscribe is only available over a websocket"),
Some(-32601),
Some(request_id),
),
vec![],
)); ));
} }
"eth_unsubscribe" => { "eth_unsubscribe" => {
return Err(anyhow::anyhow!( return Ok((
"notifications not supported. eth_unsubscribe is only available over a websocket" JsonRpcForwardedResponse::from_string(
format!("notifications not supported. eth_unsubscribe is only available over a websocket"),
Some(-32601),
Some(request_id),
),
vec![],
)); ));
} }
"net_listening" => { "net_listening" => {
@ -1342,10 +1365,18 @@ impl Web3ProxyApp {
Some(serde_json::Value::Array(params)) => { Some(serde_json::Value::Array(params)) => {
// TODO: make a struct and use serde conversion to clean this up // TODO: make a struct and use serde conversion to clean this up
if params.len() != 1 || !params[0].is_string() { if params.len() != 1 || !params[0].is_string() {
// TODO: this needs the correct error code in the response // TODO: what error code?
return Err(anyhow::anyhow!("invalid request")); return Ok((
JsonRpcForwardedResponse::from_str(
"Invalid request",
Some(-32600),
Some(request_id),
),
vec![],
));
} }
// TODO: don't return with ? here. send a jsonrpc invalid request
let param = Bytes::from_str( let param = Bytes::from_str(
params[0] params[0]
.as_str() .as_str()
@ -1359,10 +1390,27 @@ impl Web3ProxyApp {
_ => { _ => {
// TODO: this needs the correct error code in the response // TODO: this needs the correct error code in the response
// TODO: emit stat? // TODO: emit stat?
return Err(anyhow::anyhow!("invalid request")); return Ok((
JsonRpcForwardedResponse::from_str(
"invalid request",
None,
Some(request_id),
),
vec![],
));
} }
} }
} }
"test" => {
return Ok((
JsonRpcForwardedResponse::from_str(
"The method test does not exist/is not available.",
Some(-32601),
Some(request_id),
),
vec![],
));
}
// anything else gets sent to backend rpcs and cached // anything else gets sent to backend rpcs and cached
method => { method => {
// emit stats // emit stats

@ -47,7 +47,7 @@ pub fn pagerduty_alert<T: Serialize>(
timestamp: Option<OffsetDateTime>, timestamp: Option<OffsetDateTime>,
) -> AlertTrigger<T> { ) -> AlertTrigger<T> {
let client = chain_id let client = chain_id
.map(|x| format!("{} chain #{}", x, client)) .map(|x| format!("{} chain #{}", client, x))
.unwrap_or_else(|| format!("{} w/o chain", client)); .unwrap_or_else(|| format!("{} w/o chain", client));
let source = let source =