Skip to content

Commit

Permalink
add RPC error type
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Jan 1, 2025
1 parent b48c1ba commit 2470594
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
46 changes: 43 additions & 3 deletions mm2src/coins/rpc_command/tendermint/staking.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use common::PagingOptions;
use common::{HttpStatusCode, PagingOptions, StatusCode};
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::MmError;

use crate::{lp_coinfind_or_err, tendermint::TendermintCoinRpcError, MmCoinEnum};

pub type ValidatorsRPCResult = Result<ValidatorsRPCResponse, MmError<TendermintCoinRpcError>>;
pub type ValidatorsRPCResult = Result<ValidatorsRPCResponse, MmError<ValidatorsRPCError>>;

#[derive(Default, Deserialize)]
pub enum ValidatorStatus {
Expand Down Expand Up @@ -38,8 +38,48 @@ pub struct ValidatorsRPCResponse {
pub(crate) validators: Vec<serde_json::Value>,
}

#[derive(Clone, Debug, Display, Serialize, SerializeErrorType, PartialEq)]
#[serde(tag = "error_type", content = "error_data")]
pub enum ValidatorsRPCError {
#[display(fmt = "Coin '{ticker}' could not be found in coins configuration.")]
CoinNotFound { ticker: String },
#[display(fmt = "'{ticker}' is not a Cosmos coin.")]
UnexpectedCoinType { ticker: String },
#[display(fmt = "Transport error: {}", _0)]
Transport(String),
#[display(fmt = "Internal error: {}", _0)]
InternalError(String),
}

impl HttpStatusCode for ValidatorsRPCError {
fn status_code(&self) -> common::StatusCode {
match self {
ValidatorsRPCError::Transport(_) => StatusCode::SERVICE_UNAVAILABLE,
ValidatorsRPCError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
ValidatorsRPCError::UnexpectedCoinType { .. } | ValidatorsRPCError::CoinNotFound { .. } => {
StatusCode::BAD_REQUEST
},
}
}
}

impl From<TendermintCoinRpcError> for ValidatorsRPCError {
fn from(e: TendermintCoinRpcError) -> Self {
match e {
TendermintCoinRpcError::InvalidResponse(e)
| TendermintCoinRpcError::PerformError(e)
| TendermintCoinRpcError::RpcClientError(e) => ValidatorsRPCError::Transport(e),
TendermintCoinRpcError::Prost(e) | TendermintCoinRpcError::InternalError(e) => ValidatorsRPCError::InternalError(e),
TendermintCoinRpcError::UnexpectedAccountType { .. } => ValidatorsRPCError::InternalError(
"RPC client got an unexpected error 'TendermintCoinRpcError::UnexpectedAccountType', this isn't normal."
.into(),
),
}
}
}

#[inline(always)]
pub async fn validators_list_rpc(ctx: MmArc, req: ValidatorsRPC) -> ValidatorsRPCResult {
pub async fn validators_rpc(ctx: MmArc, req: ValidatorsRPC) -> ValidatorsRPCResult {
let validators = match lp_coinfind_or_err(&ctx, &req.coin).await {
Ok(MmCoinEnum::Tendermint(coin)) => coin.validators_list(req.filter_by_status, req.paging).await?,
Ok(MmCoinEnum::TendermintToken(token)) => {
Expand Down
20 changes: 0 additions & 20 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,6 @@ pub enum TendermintCoinRpcError {
UnexpectedAccountType {
prefix: String,
},
#[display(fmt = "Coin '{ticker}' could not be found in coins configuration.")]
CoinNotFound {
ticker: String,
},
#[display(fmt = "'{ticker}' is not a Cosmos coin.")]
UnexpectedCoinType {
ticker: String,
},
}

impl From<DecodeError> for TendermintCoinRpcError {
Expand All @@ -475,12 +467,6 @@ impl From<TendermintCoinRpcError> for BalanceError {
TendermintCoinRpcError::UnexpectedAccountType { prefix } => {
BalanceError::Internal(format!("Account type '{prefix}' is not supported for HTLCs"))
},
TendermintCoinRpcError::CoinNotFound { ticker } => {
BalanceError::Internal(format!("Coin '{ticker}' could not be found in coins configuration."))
},
TendermintCoinRpcError::UnexpectedCoinType { ticker } => {
BalanceError::Internal(format!("'{ticker}' is not a Cosmos coin."))
},
}
}
}
Expand All @@ -497,12 +483,6 @@ impl From<TendermintCoinRpcError> for ValidatePaymentError {
TendermintCoinRpcError::UnexpectedAccountType { prefix } => {
ValidatePaymentError::InvalidParameter(format!("Account type '{prefix}' is not supported for HTLCs"))
},
TendermintCoinRpcError::CoinNotFound { ticker } => ValidatePaymentError::InvalidParameter(format!(
"Coin '{ticker}' could not be found in coins configuration."
)),
TendermintCoinRpcError::UnexpectedCoinType { ticker } => {
ValidatePaymentError::InvalidParameter(format!("'{ticker}' is not a Cosmos coin."))
},
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::rpc::lp_commands::trezor::trezor_connection_status;
use crate::rpc::rate_limiter::{process_rate_limit, RateLimitContext};
use coins::eth::EthCoin;
use coins::my_tx_history_v2::my_tx_history_v2_rpc;
use coins::rpc_command::tendermint::staking::validators_rpc;
use coins::rpc_command::tendermint::{ibc_chains, ibc_transfer_channels};
use coins::rpc_command::{account_balance::account_balance,
get_current_mtp::get_current_mtp_rpc,
Expand Down Expand Up @@ -212,6 +213,7 @@ async fn dispatcher_v2(request: MmRpcRequest, ctx: MmArc) -> DispatcherResult<Re
"start_version_stat_collection" => handle_mmrpc(ctx, request, start_version_stat_collection).await,
"stop_simple_market_maker_bot" => handle_mmrpc(ctx, request, stop_simple_market_maker_bot).await,
"stop_version_stat_collection" => handle_mmrpc(ctx, request, stop_version_stat_collection).await,
"tendermint_validators" => handle_mmrpc(ctx, request, validators_rpc).await,
"trade_preimage" => handle_mmrpc(ctx, request, trade_preimage_rpc).await,
"trezor_connection_status" => handle_mmrpc(ctx, request, trezor_connection_status).await,
"update_nft" => handle_mmrpc(ctx, request, update_nft).await,
Expand Down

0 comments on commit 2470594

Please sign in to comment.