use alloc::boxed::Box;
use keetanetwork_account::AccountError;
use keetanetwork_block::BlockError;
use keetanetwork_error::KeetaNetError;
use keetanetwork_vote::VoteError;
use num_bigint::ParseBigIntError;
use snafu::Snafu;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum ClientError {
#[snafu(display("node request failed"))]
Transport {
#[cfg(not(target_family = "wasm"))]
source: Box<dyn core::error::Error + Send + Sync>,
#[cfg(target_family = "wasm")]
source: Box<dyn core::error::Error>,
},
#[snafu(display("node returned an error"))]
Node {
source: Box<KeetaNetError>,
},
#[snafu(display("malformed base64 in node response"))]
Decode {
source: base64::DecodeError,
},
#[snafu(display("vote decoding or validation failed"))]
Vote {
source: VoteError,
},
#[snafu(display("block decoding failed"))]
Block {
source: BlockError,
},
#[snafu(display("malformed amount in node response"))]
Amount {
source: ParseBigIntError,
},
#[snafu(display("node response omitted the vote"))]
MissingVote,
#[snafu(display("node response omitted the vote quote"))]
MissingQuote,
#[snafu(display("node response omitted the publish result"))]
MissingPublish,
#[snafu(display("node response omitted the version"))]
MissingVersion,
#[snafu(display("node votes require a fee block but no signer was supplied"))]
FeeRequired,
#[snafu(display("network base token derivation failed"))]
Account {
source: AccountError,
},
#[snafu(display("network id is unset or unsupported for fee token derivation"))]
UnsupportedNetwork,
#[snafu(display("no representatives available"))]
NoRepresentatives,
#[snafu(display("request timed out"))]
Timeout,
#[snafu(display("could not reach voting quorum"))]
QuorumNotReached,
#[snafu(display("sync found a missing staple but could not publish it"))]
SyncPublishFailed,
#[snafu(display("account recovery failed"))]
RecoverFailed,
#[snafu(display("identifier is unresolved until its builder is built"))]
UnresolvedIdentifier,
#[snafu(display("operation requires a signer but none is bound"))]
SignerRequired,
#[snafu(display("swap request must render to exactly one block"))]
SwapMultiBlock,
#[snafu(display("swap request is missing a send operation"))]
SwapMissingSend,
#[snafu(display("swap request is missing a receive operation"))]
SwapMissingReceive,
#[snafu(display("swap request accounts do not match"))]
SwapAccountMismatch,
#[snafu(display("swap request token does not match expected"))]
SwapTokenMismatch,
#[snafu(display("swap request amount does not match expected"))]
SwapAmountMismatch,
#[snafu(display("swap send amount is below the requested receive amount"))]
SwapAmountTooLow,
#[snafu(display("swap send amount differs from an exact receive amount"))]
SwapExactMismatch,
}
impl ClientError {
pub fn code(&self) -> &str {
match self {
Self::Transport { .. } => "TRANSPORT",
Self::Node { source } => source.code().unwrap_or("NODE"),
Self::Decode { .. } => "DECODE",
Self::Vote { .. } => "VOTE",
Self::Block { .. } => "BLOCK",
Self::Amount { .. } => "AMOUNT",
Self::MissingVote => "MISSING_VOTE",
Self::MissingQuote => "MISSING_QUOTE",
Self::MissingPublish => "MISSING_PUBLISH",
Self::MissingVersion => "MISSING_VERSION",
Self::FeeRequired => "FEE_REQUIRED",
Self::Account { .. } => "ACCOUNT",
Self::UnsupportedNetwork => "UNSUPPORTED_NETWORK",
Self::NoRepresentatives => "NO_REPRESENTATIVES",
Self::Timeout => "TIMEOUT",
Self::QuorumNotReached => "QUORUM_NOT_REACHED",
Self::SyncPublishFailed => "SYNC_PUBLISH_FAILED",
Self::RecoverFailed => "RECOVER_FAILED",
Self::UnresolvedIdentifier => "UNRESOLVED_IDENTIFIER",
Self::SignerRequired => "SIGNER_REQUIRED",
Self::SwapMultiBlock => "SWAP_MULTI_BLOCK",
Self::SwapMissingSend => "SWAP_MISSING_SEND",
Self::SwapMissingReceive => "SWAP_MISSING_RECEIVE",
Self::SwapAccountMismatch => "SWAP_ACCOUNT_MISMATCH",
Self::SwapTokenMismatch => "SWAP_TOKEN_MISMATCH",
Self::SwapAmountMismatch => "SWAP_AMOUNT_MISMATCH",
Self::SwapAmountTooLow => "SWAP_AMOUNT_TOO_LOW",
Self::SwapExactMismatch => "SWAP_EXACT_MISMATCH",
}
}
}