use spl_token::solana_program::program_error::ProgramError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ToolkitError {
#[error("RPC error: {0}")]
RpcError(#[from] solana_client::client_error::ClientError),
#[error("Transaction error: {0}")]
TransactionError(String),
#[error("Account not found: {0}")]
AccountNotFound(String),
#[error("Invalid account data: {0}")]
InvalidAccountData(String),
#[error("Deserialization error: {0}")]
Deserialization(String),
#[error("Insufficient balance: need {needed}, have {available}")]
InsufficientBalance {
needed: u64,
available: u64,
},
#[error("Invalid PDA: {0}")]
InvalidPda(String),
#[error("Token error: {0}")]
TokenError(String),
#[error("Program error: {0}")]
ProgramError(#[from] ProgramError),
#[error("Signature error: {0}")]
SignatureError(#[from] solana_sdk::signature::SignerError),
#[error("Network error: {0}")]
NetworkError(String),
#[error("Parse error: {0}")]
ParseError(String),
#[error("Jupiter error: {0}")]
JupiterError(String),
#[error("Signing error: {0}")]
SigningError(String),
#[error("Batch error: {successful} succeeded, {failed} failed")]
BatchError {
successful: usize,
failed: usize,
errors: Vec<String>,
},
#[error("Graph error: {0}")]
GraphError(String),
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("Operation timed out: {0}")]
Timeout(String),
#[error("Custom error: {0}")]
Custom(String),
}
impl ToolkitError {
pub fn transaction<S: Into<String>>(msg: S) -> Self {
Self::TransactionError(msg.into())
}
pub fn account_not_found<S: Into<String>>(pubkey: S) -> Self {
Self::AccountNotFound(pubkey.into())
}
pub fn invalid_data<S: Into<String>>(msg: S) -> Self {
Self::InvalidAccountData(msg.into())
}
pub fn custom<S: Into<String>>(msg: S) -> Self {
Self::Custom(msg.into())
}
pub fn is_retryable(&self) -> bool {
matches!(
self,
Self::RpcError(_) | Self::NetworkError(_) | Self::Timeout(_)
)
}
}
pub type Result<T> = std::result::Result<T, ToolkitError>;