use std::{io::Error, num::TryFromIntError};
use diesel::r2d2;
use tari_utilities::{hex::HexError, message_format::MessageFormatError};
use thiserror::Error;
use tokio::task;
#[derive(Debug, Error)]
pub enum SqliteStorageError {
#[error("Poolsize is too big")]
PoolSize(#[from] TryFromIntError),
#[error("Database error: `{0}`")]
R2d2Error(#[from] r2d2::Error),
#[error("Database error: `{0}`")]
DieselR2d2Error(String),
#[error("Database error: `{0}`")]
DieselConnectionError(#[from] diesel::ConnectionError),
}
#[derive(Debug, Error)]
pub enum StorageError {
#[error("ConnectionError: {0}")]
ConnectionError(#[from] diesel::ConnectionError),
#[error("Error when joining to tokio task: {0}")]
JoinError(#[from] task::JoinError),
#[error("DatabaseMigrationFailed: {0}")]
DatabaseMigrationFailed(String),
#[error("Diesel result error: {0}")]
DieselResultError(#[from] diesel::result::Error),
#[error("MessageFormatError: {0}")]
MessageFormatError(String),
#[error("Unexpected result: {0}")]
UnexpectedResult(String),
#[error("Diesel R2d2 error: `{0}`")]
DieselR2d2Error(#[from] SqliteStorageError),
#[error("Hex conversion error: `{0}`")]
HexError(String),
#[error("JSON error: {0}")]
JsonError(#[from] serde_json::Error),
#[error("TryFromInt conversion error: `{0}`")]
TryFromIntError(#[from] TryFromIntError),
#[error("IO error: `{0}`")]
IoError(#[from] Error),
#[error("Database migration lock error: {0}")]
DatabaseMigrationLockError(String),
}
impl From<MessageFormatError> for StorageError {
fn from(value: MessageFormatError) -> Self {
StorageError::MessageFormatError(value.to_string())
}
}
impl From<HexError> for StorageError {
fn from(value: HexError) -> Self {
StorageError::HexError(value.to_string())
}
}