Skip to content

Commit

Permalink
Refactor error handling in get_transaction_status
Browse files Browse the repository at this point in the history
Clarified error variants by introducing an Error enum and implemented conversions from anyhow::Error and SequencerError. This enhances the readability and maintainability of the code by specifying distinct error types and their mappings.
  • Loading branch information
steebchen committed Aug 16, 2024
1 parent df4cc97 commit a117fb9
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion crates/rpc/src/method/get_transaction_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,40 @@ pub enum Output {
AcceptedOnL2(TxnExecutionStatus),
}

crate::error::generate_rpc_error_subset!(Error: TxnHashNotFound);
#[derive(Debug)]
pub enum Error {
Internal(anyhow::Error),
TxnHashNotFound,
GatewayError(starknet_gateway_types::error::StarknetError),
}

impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Self {
Self::Internal(e)
}
}

impl From<starknet_gateway_types::error::SequencerError> for Error {
fn from(e: starknet_gateway_types::error::SequencerError) -> Self {
use starknet_gateway_types::error::SequencerError::*;

match e {
StarknetError(e) => Error::GatewayError(e),
ReqwestError(e) => Error::Internal(e.into()),
InvalidStarknetErrorVariant => Error::Internal(anyhow::anyhow!("Invalid error variant received from gateway")),
}
}
}

impl From<Error> for crate::error::ApplicationError {
fn from(e: Error) -> Self {
match e {
Error::Internal(internal) => Self::Internal(internal),
Error::TxnHashNotFound => Self::TxnHashNotFound,
Error::GatewayError(gateway_error) => Self::GatewayError(gateway_error),
}
}
}

pub async fn get_transaction_status(context: RpcContext, input: Input) -> Result<Output, Error> {
// Check database.
Expand Down

0 comments on commit a117fb9

Please sign in to comment.