From 16abd0fc186dedd18054fab9870f931b3b5b9f10 Mon Sep 17 00:00:00 2001 From: Eshaan Bansal Date: Thu, 6 Feb 2025 17:03:18 +0530 Subject: [PATCH] return 500 status code for errors --- verifiable-api/bin/handlers.rs | 71 +++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/verifiable-api/bin/handlers.rs b/verifiable-api/bin/handlers.rs index b7520cc7..0267cb35 100644 --- a/verifiable-api/bin/handlers.rs +++ b/verifiable-api/bin/handlers.rs @@ -9,9 +9,10 @@ use alloy::{ }; use axum::{ extract::{Path, Query, State}, + http::StatusCode, response::Json, }; -use eyre::Result; +use eyre::{Report, Result}; use serde::Deserialize; use serde_json::json; @@ -24,6 +25,10 @@ fn json_err(error: &str) -> Json { Json(json!({ "error": error })) } +fn map_server_err(e: Report) -> (StatusCode, Json) { + (StatusCode::INTERNAL_SERVER_ERROR, json_err(&e.to_string())) +} + #[derive(Deserialize)] pub struct BlockQuery { block: Option, @@ -37,14 +42,14 @@ pub async fn get_balance>( Path(address): Path
, Query(BlockQuery { block }): Query, State(ApiState { execution_client }): State>, -) -> Result, Json> { +) -> Result, (StatusCode, Json)> { let block = block.unwrap_or(BlockId::latest()); let proof = execution_client .rpc .get_proof(address, &[], block) .await - .map_err(|e| json_err(&e.to_string()))?; + .map_err(map_server_err)?; Ok(Json(GetBalanceResponse { account: Account { @@ -65,14 +70,14 @@ pub async fn get_transaction_count>( Path(address): Path
, Query(BlockQuery { block }): Query, State(ApiState { execution_client }): State>, -) -> Result, Json> { +) -> Result, (StatusCode, Json)> { let block = block.unwrap_or(BlockId::latest()); let proof = execution_client .rpc .get_proof(address, &[], block) .await - .map_err(|e| json_err(&e.to_string()))?; + .map_err(map_server_err)?; Ok(Json(GetTransactionCountResponse { account: Account { @@ -93,7 +98,7 @@ pub async fn get_code>( Path(address): Path
, Query(BlockQuery { block }): Query, State(ApiState { execution_client }): State>, -) -> Result, Json> { +) -> Result, (StatusCode, Json)> { // Ensure that BlockId is of block number variant let block = block.unwrap_or(BlockId::latest()); let block_num = match block { @@ -103,15 +108,20 @@ pub async fn get_code>( .rpc .get_block_by_number(tag, BlockTransactionsKind::Hashes) .await - .map_err(|e| json_err(&e.to_string()))? - .ok_or_else(|| json_err("Block not found")) + .map_err(map_server_err)? + .ok_or_else(|| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + json_err("Block not found"), + ) + }) .map(|block| block.header().number()), }, BlockId::Hash(hash) => execution_client .rpc .get_block(hash.into()) .await - .map_err(|e| json_err(&e.to_string())) + .map_err(map_server_err) .map(|block| block.header().number()), }?; let block = BlockId::from(block_num); @@ -120,13 +130,13 @@ pub async fn get_code>( .rpc .get_proof(address, &[], block) .await - .map_err(|e| json_err(&e.to_string()))?; + .map_err(map_server_err)?; let code = execution_client .rpc .get_code(address, block_num) .await - .map_err(|e| json_err(&e.to_string()))?; + .map_err(map_server_err)?; Ok(Json(GetCodeResponse { code: code.into(), @@ -149,24 +159,29 @@ pub async fn get_storage_at>( Path((address, key)): Path<(Address, U256)>, Query(BlockQuery { block }): Query, State(ApiState { execution_client }): State>, -) -> Result, Json> { +) -> Result, (StatusCode, Json)> { let block = block.unwrap_or(BlockId::latest()); let storage_slot = execution_client .rpc .get_storage_at(address, key, block) .await - .map_err(|e| json_err(&e.to_string()))?; + .map_err(map_server_err)?; let proof = execution_client .rpc .get_proof(address, &[storage_slot.into()], block) .await - .map_err(|e| json_err(&e.to_string()))?; + .map_err(map_server_err)?; let storage = match proof.storage_proof.get(0) { Some(storage) => storage.clone(), - None => return Err(json_err("Failed to get storage proof")), + None => { + return Err(( + StatusCode::INTERNAL_SERVER_ERROR, + json_err("Failed to get storage proof"), + )) + } }; Ok(Json(GetStorageAtResponse { @@ -187,20 +202,30 @@ pub async fn get_storage_at>( pub async fn get_transaction_receipt>( Path(tx_hash): Path, State(ApiState { execution_client }): State>, -) -> Result>, Json> { +) -> Result>, (StatusCode, Json)> { let receipt = execution_client .rpc .get_transaction_receipt(tx_hash) .await - .map_err(|e| json_err(&e.to_string()))? - .ok_or_else(|| json_err("Transaction not found"))?; + .map_err(map_server_err)? + .ok_or_else(|| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + json_err("Transaction not found"), + ) + })?; let receipts = execution_client .rpc .get_block_receipts(BlockTag::Number(receipt.block_number().unwrap())) .await - .map_err(|e| json_err(&e.to_string()))? - .ok_or_else(|| json_err("No receipts found for the block"))?; + .map_err(map_server_err)? + .ok_or_else(|| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + json_err("No receipts found for the block"), + ) + })?; let receipt_proof = create_receipt_proof::(receipts, receipt.transaction_index().unwrap() as usize); @@ -218,13 +243,15 @@ pub async fn get_transaction_receipt>( pub async fn get_filter_logs>( Path(filter_id): Path, State(ApiState { execution_client }): State>, -) -> Result>, Json> { +) -> Result>, (StatusCode, Json)> { + // Fetch the filter logs from RPC let logs = execution_client .rpc .get_filter_logs(filter_id) .await - .map_err(|e| json_err(&e.to_string()))?; + .map_err(map_server_err)?; + // Create a map of transaction hashes to their receipts and proofs let mut receipt_proofs: HashMap> = HashMap::new(); // ToDo(@eshaan7): Optimise this by fetching receipts once per block