use std::io;
use std::str::Utf8Error;
use hex::FromHexError;
use inquire::InquireError;
use node_data::bls::ConsensusKeysError;
use rand::Error as RngError;
use crate::gql::GraphQLError;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("This command cannot be performed while offline")]
Offline,
#[error("Unauthorized access to this address")]
Unauthorized,
#[error("Rusk error occurred: {0}")]
Rusk(String),
#[error(transparent)]
IO(#[from] io::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error("A serialization error occurred: {0:?}")]
Bytes(dusk_bytes::Error),
#[error(transparent)]
Base58(#[from] bs58::decode::Error),
#[error("A serialization error occurred.")]
Rkyv,
#[error("Invalid Hex data: {0}")]
Hex(#[from] FromHexError),
#[error("Cannot create HTTP client")]
HttpClient,
#[error("A request error occurred: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("Utf8 error: {0:?}")]
Utf8(Utf8Error),
#[error(transparent)]
Rng(#[from] RngError),
#[error("Insufficient balance to perform this operation")]
NotEnoughBalance,
#[error("Amount to transfer/stake cannot be zero")]
AmountIsZero,
#[error("Impossible notes' combination for the given value is")]
NoteCombinationProblem,
#[error("Note wasn't found in transfer-contract")]
NoteNotFound,
#[error("Note couldn't be decrypted with the provided ViewKey")]
WrongViewKey,
#[error("Not enough gas to perform this transaction")]
NotEnoughGas,
#[error("A stake does not exist for this key")]
NotStaked,
#[error("No reward available for this key")]
NoReward,
#[error("Invalid address")]
BadAddress,
#[error("Address does not belong to this wallet")]
AddressNotOwned,
#[error("No menu item selected")]
NoMenuItemSelected,
#[error("Invalid mnemonic phrase")]
InvalidMnemonicPhrase,
#[error("Path provided is not a directory")]
NotDirectory,
#[error("OS not supported")]
OsNotSupported,
#[error("Wallet file content is not valid")]
WalletFileCorrupted,
#[error("File version {0}.{1} not recognized")]
UnknownFileVersion(u8, u8),
#[error("A wallet file with this name already exists")]
WalletFileExists,
#[error("Wallet file is missing")]
WalletFileMissing,
#[error("Invalid password")]
BlockMode(#[from] block_modes::BlockModeError),
#[error("Encryption error: {0}")]
Encryption(#[from] aes_gcm::Error),
#[error("Reached the maximum number of attempts")]
AttemptsExhausted,
#[error("Status callback needs to be set before connecting")]
StatusWalletConnected,
#[error("Transaction error: {0}")]
Transaction(String),
#[error("Rocks cache database error: {0}")]
RocksDB(rocksdb::Error),
#[error(
"Network not found, check config.toml, specify network with -n flag"
)]
NetworkNotFound,
#[error("Cache database corrupted")]
CacheDatabaseCorrupted,
#[error("Prover Error: {0}")]
ProverError(String),
#[error("Memo too large {0}")]
MemoTooLarge(usize),
#[error("Expected BLS Public Key")]
ExpectedBlsPublicKey,
#[error("Expected Phoenix public Key")]
ExpectedPhoenixPublicKey,
#[error("Addresses use different transaction models")]
DifferentTransactionModels,
#[error("Invalid contractID provided")]
InvalidContractId,
#[error("Invalid WASM contract path provided")]
InvalidWasmContractPath,
#[error("Invalid environment variable value {0}")]
InvalidEnvVar(String),
#[error("Error while processing blob data: {0}")]
Blob(String),
#[error("Conversion error: {0}")]
Conversion(String),
#[error("GraphQL error: {0}")]
GraphQLError(GraphQLError),
#[error("Inquire error: {0}")]
InquireError(String),
#[error("Archive node query error: {0}")]
ArchiveJsonError(String),
#[error("Error while saving consensus keys: {0}")]
ConsensusKeysError(ConsensusKeysError),
#[error("Trying to claim more than existing reward")]
NotEnoughReward,
}
impl From<dusk_bytes::Error> for Error {
fn from(e: dusk_bytes::Error) -> Self {
Self::Bytes(e)
}
}
impl From<block_modes::InvalidKeyIvLength> for Error {
fn from(_: block_modes::InvalidKeyIvLength) -> Self {
Self::WalletFileCorrupted
}
}
impl From<dusk_core::Error> for Error {
fn from(e: dusk_core::Error) -> Self {
match e {
dusk_core::Error::InsufficientBalance => Self::NotEnoughBalance,
dusk_core::Error::Replay => Self::Transaction("Replay".to_string()),
dusk_core::Error::PhoenixOwnership => Self::AddressNotOwned,
dusk_core::Error::PhoenixCircuit(s)
| dusk_core::Error::PhoenixProver(s) => Self::ProverError(s),
dusk_core::Error::InvalidData => {
Self::Bytes(dusk_bytes::Error::InvalidData)
}
dusk_core::Error::BadLength(found, expected) => {
Self::Bytes(dusk_bytes::Error::BadLength { found, expected })
}
dusk_core::Error::InvalidChar(ch, index) => {
Self::Bytes(dusk_bytes::Error::InvalidChar { ch, index })
}
dusk_core::Error::Rkyv(_) => Self::Rkyv,
dusk_core::Error::MemoTooLarge(m) => Self::MemoTooLarge(m),
dusk_core::Error::Blob(s) => Self::Blob(s),
}
}
}
impl From<rocksdb::Error> for Error {
fn from(e: rocksdb::Error) -> Self {
Self::RocksDB(e)
}
}
impl From<GraphQLError> for Error {
fn from(e: GraphQLError) -> Self {
Self::GraphQLError(e)
}
}
impl From<InquireError> for Error {
fn from(e: InquireError) -> Self {
Self::InquireError(e.to_string())
}
}
impl From<node_data::bls::ConsensusKeysError> for Error {
fn from(e: ConsensusKeysError) -> Self {
Self::ConsensusKeysError(e)
}
}