1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use super::{AsyncDatabase, AsyncPendingManager};

/// Error type for the transaction.
#[derive(thiserror::Error)]
pub enum TransactionError<P: AsyncPendingManager> {
  /// Returned if an update function is called on a read-only transaction.
  #[error("transaction is read-only")]
  ReadOnly,

  /// Returned when a transaction conflicts with another transaction. This can
  /// happen if the read rows had been updated concurrently by another transaction.
  #[error("transaction conflict, please retry")]
  Conflict,

  /// Returned if a previously discarded transaction is re-used.
  #[error("transaction has been discarded, please create a new one")]
  Discard,

  /// Returned if too many writes are fit into a single transaction.
  #[error("transaction is too large")]
  LargeTxn,

  /// Returned if the transaction manager error occurs.
  #[error("transaction manager error: {0}")]
  Manager(P::Error),
}

impl<P: AsyncPendingManager> core::fmt::Debug for TransactionError<P> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::ReadOnly => write!(f, "ReadOnly"),
      Self::Conflict => write!(f, "Conflict"),
      Self::Discard => write!(f, "Discard"),
      Self::LargeTxn => write!(f, "LargeTxn"),
      Self::Manager(e) => write!(f, "Manager({:?})", e),
    }
  }
}

/// Error type for the [`TransactionDB`].
#[derive(thiserror::Error)]
pub enum Error<D: AsyncDatabase, P: AsyncPendingManager> {
  /// Returned if transaction related error occurs.
  #[error(transparent)]
  Transaction(#[from] TransactionError<P>),

  /// Returned if DB related error occurs.
  #[error(transparent)]
  DB(D::Error),
}

impl<D: AsyncDatabase, P: AsyncPendingManager> core::fmt::Debug for Error<D, P> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Transaction(e) => e.fmt(f),
      Self::DB(e) => e.fmt(f),
    }
  }
}

impl<D: AsyncDatabase, P: AsyncPendingManager> Error<D, P> {
  /// Create a new error from the database error.
  pub fn database(err: D::Error) -> Self {
    Self::DB(err)
  }

  /// Create a new error from the transaction error.
  pub fn transaction(err: TransactionError<P>) -> Self {
    Self::Transaction(err)
  }
}