use std::fmt::Debug;
use serde::{
ser::{SerializeMap, Serializer},
Serialize,
};
use crate::types::block::payload::transaction::TransactionId;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("can't create account: account alias {0} already exists")]
AccountAliasAlreadyExists(String),
#[error("account {0} not found")]
AccountNotFound(String),
#[error("address {0} not found in account")]
AddressNotFoundInAccount(String),
#[error("backup failed {0}")]
Backup(&'static str),
#[error("{0}")]
Block(Box<crate::types::block::Error>),
#[error("burning or melting failed: {0}")]
BurningOrMeltingFailed(String),
#[error("`{0}`")]
Client(Box<crate::client::Error>),
#[error("funds are spread over too many outputs {output_count}/{output_count_max}, consolidation required")]
ConsolidationRequired { output_count: usize, output_count_max: u16 },
#[error("{0}")]
Crypto(#[from] crypto::Error),
#[error("custom input error {0}")]
CustomInput(String),
#[error("failed to get remainder address")]
FailedToGetRemainder,
#[error("insufficient funds {available}/{required} available")]
InsufficientFunds { available: u64, required: u64 },
#[error("invalid coin type for new account: {new_coin_type}, existing coin type is: {existing_coin_type}")]
InvalidCoinType {
new_coin_type: u32,
existing_coin_type: u32,
},
#[error("invalid mnemonic: {0}")]
InvalidMnemonic(String),
#[error("invalid output kind: {0}")]
InvalidOutputKind(String),
#[error("`{0}`")]
Io(#[from] std::io::Error),
#[error("`{0}`")]
Json(#[from] serde_json::error::Error),
#[error("minting failed {0}")]
MintingFailed(String),
#[error("missing parameter: {0}")]
MissingParameter(&'static str),
#[error("nft not found in unspent outputs")]
NftNotFoundInUnspentOutputs,
#[cfg(feature = "participation")]
#[cfg_attr(docsrs, doc(cfg(feature = "participation")))]
#[error("voting error {0}")]
Voting(String),
#[cfg(feature = "participation")]
#[cfg_attr(docsrs, doc(cfg(feature = "participation")))]
#[error("participation error {0}")]
Participation(#[from] crate::types::api::plugins::participation::error::Error),
#[error(
"nothing to consolidate: available outputs: {available_outputs}, consolidation threshold: {consolidation_threshold}"
)]
NoOutputsToConsolidate {
available_outputs: usize,
consolidation_threshold: usize,
},
#[error("error accessing storage: {0}")]
Storage(String),
#[error("can't perform operation while storage is encrypted; use Wallet::set_storage_password to decrypt storage")]
StorageIsEncrypted,
#[error("{0}")]
TaskJoin(#[from] tokio::task::JoinError),
#[error("transaction {0} not found")]
TransactionNotFound(TransactionId),
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_map(Some(2))?;
let mut kind_dbg = format!("{self:?}");
if let Some(r) = kind_dbg.get_mut(0..1) {
r.make_ascii_lowercase();
}
let kind = kind_dbg.split([' ', '(']).next().unwrap();
seq.serialize_entry("type", &kind)?;
seq.serialize_entry("error", &self.to_string())?;
seq.end()
}
}
impl From<crate::types::block::Error> for Error {
fn from(error: crate::types::block::Error) -> Self {
Self::Block(Box::new(error))
}
}
impl From<crate::client::Error> for Error {
fn from(error: crate::client::Error) -> Self {
Self::Client(Box::new(error))
}
}
impl From<crate::client::api::input_selection::Error> for Error {
fn from(error: crate::client::api::input_selection::Error) -> Self {
Self::Client(Box::new(crate::client::Error::InputSelection(error)))
}
}
#[cfg(feature = "stronghold")]
impl From<crate::client::stronghold::Error> for Error {
fn from(error: crate::client::stronghold::Error) -> Self {
Self::Client(Box::new(crate::client::Error::Stronghold(error)))
}
}
#[cfg(feature = "ledger_nano")]
impl From<crate::client::secret::ledger_nano::Error> for Error {
fn from(error: crate::client::secret::ledger_nano::Error) -> Self {
Self::Client(Box::new(crate::client::Error::Ledger(error)))
}
}
#[cfg(feature = "rocksdb")]
impl From<rocksdb::Error> for Error {
fn from(error: rocksdb::Error) -> Self {
Self::Storage(error.to_string())
}
}