sdk_common/src/error.rs
2024-05-27 11:54:19 +02:00

42 lines
1.2 KiB
Rust

use std::fmt;
use std::error::Error;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AnkError {
GenericError(String),
FaucetError(String),
NewTxError(String),
CipherError(String),
}
impl fmt::Display for AnkError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AnkError::GenericError(msg) => write!(f, "GenericError: {}", msg),
AnkError::FaucetError(msg) => write!(f, "FaucetError: {}", msg),
AnkError::NewTxError(msg) => write!(f, "NewTxError: {}", msg),
AnkError::CipherError(msg) => write!(f, "CipherError: {}", msg),
}
}
}
impl Error for AnkError {}
impl From<anyhow::Error> for AnkError {
fn from(error: anyhow::Error) -> Self {
let error_message = error.to_string();
if error_message.contains("FaucetError") {
AnkError::FaucetError(error_message)
} else if error_message.contains("NewTxError") {
AnkError::NewTxError(error_message)
} else if error_message.contains("CipherError") {
AnkError::CipherError(error_message)
} else {
AnkError::GenericError(error_message)
}
}
}