Add error mod

This commit is contained in:
Sosthene 2024-05-27 11:52:32 +02:00
parent 72089b8545
commit 6492952dc0
3 changed files with 43 additions and 0 deletions

41
src/error.rs Normal file
View File

@ -0,0 +1,41 @@
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)
}
}
}

View File

@ -1,3 +1,4 @@
pub mod crypto; pub mod crypto;
pub mod network; pub mod network;
pub mod silentpayments; pub mod silentpayments;
pub mod error;

View File

@ -8,6 +8,7 @@ use sp_client::silentpayments::bitcoin_hashes::{sha256t_hash_newtype, Hash, Hash
use tsify::Tsify; use tsify::Tsify;
use crate::crypto::{Aes256Decryption, CipherText, Purpose}; use crate::crypto::{Aes256Decryption, CipherText, Purpose};
use crate::error::AnkError;
const RAWTXTOPIC: &'static str = "rawtx"; const RAWTXTOPIC: &'static str = "rawtx";
const RAWBLOCKTOPIC: &'static str = "rawblock"; const RAWBLOCKTOPIC: &'static str = "rawblock";