use thiserror::Error;
#[derive(Error, Debug)]
pub enum SdkError {
#[error("{0}")]
User(#[from] UserError),
#[error("{0}")]
Api(#[from] APIError),
#[error("{0}")]
Transaction(String),
#[error("{0}")]
Protocol(#[from] ProtocolError),
}
#[derive(Error, Debug)]
pub enum UserError {
#[error("{0}")]
InvalidInputs(String),
#[error("{0}")]
Other(String),
}
#[derive(Error, Debug)]
pub struct APIError {
message: String,
status_code: i32,
}
impl APIError {
pub fn new(message: String, status_code: i32) -> Self {
Self {
message,
status_code,
}
}
}
impl std::fmt::Display for APIError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "API Error ({}): {}", self.status_code, self.message)
}
}
#[derive(Error, Debug)]
pub struct ProtocolError {
message: String,
}
impl ProtocolError {
pub fn new(message: String) -> Self {
Self { message }
}
}
impl std::fmt::Display for ProtocolError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Protocol Error: {}", self.message)
}
}
#[derive(Debug)]
pub struct Transaction(pub String);
impl std::fmt::Display for Transaction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Transaction error: {}", self.0)
}
}
impl std::error::Error for Transaction {}
impl From<Transaction> for SdkError {
fn from(err: Transaction) -> Self {
SdkError::Transaction(err.0)
}
}
pub type Result<T> = std::result::Result<T, SdkError>;
pub use UserError::InvalidInputs as InvalidInputsError;