use thiserror::Error;
#[derive(Error, Debug)]
pub enum TransactionError {
#[error("Failed to create transaction: {0}")]
CreationFailed(String),
#[error("Failed to commit transaction: {0}")]
CommitFailed(String),
#[error("Failed to rollback transaction: {0}")]
RollbackFailed(String),
#[error("Transaction timeout after {0} seconds")]
Timeout(u64),
#[error("Invalid transaction state: {0}")]
InvalidState(String),
#[error("Transaction not found: {0}")]
NotFound(String),
#[error("Concurrent modification: {0}")]
ConcurrentModification(String),
#[error("Deadlock detected: {0}")]
Deadlock(String),
#[error("Database error: {0}")]
Database(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Transaction error: {0}")]
Other(String),
}
pub type TransactionResult<T> = Result<T, TransactionError>;
#[derive(Error, Debug)]
#[error("Transaction system exception: {message}")]
pub(crate) struct TransactionSystemException {
pub message: String,
#[source]
pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl TransactionSystemException {
pub(crate) fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
source: None,
}
}
pub(crate) fn with_source(
message: impl Into<String>,
source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> Self {
Self {
message: message.into(),
source: Some(source.into()),
}
}
}
#[derive(Error, Debug)]
#[error("Unexpected rollback: {message}")]
pub(crate) struct UnexpectedRollbackException {
pub message: String,
}
impl UnexpectedRollbackException {
pub(crate) fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}