Skip to main content

miden_note_transport_node/database/
error.rs

1use thiserror::Error;
2
3/// Database-specific error types
4#[derive(Debug, Error)]
5pub enum DatabaseError {
6    /// Configuration error
7    #[error("Configuration error: {0}")]
8    Configuration(String),
9
10    /// Connection error
11    #[error("Connection error: {0}")]
12    Connection(String),
13
14    /// Migration error
15    #[error("Migration error: {0}")]
16    Migration(String),
17
18    /// Query execution error
19    #[error("Query execution error: {0}")]
20    QueryExecution(String),
21
22    /// Serialization error
23    #[error("Serialization error: {0}")]
24    Serialization(String),
25
26    /// Deserialization error
27    #[error("Deserialization error: {0}")]
28    Deserialization(String),
29
30    /// Constraint violation error
31    #[error("Constraint violation: {0}")]
32    ConstraintViolation(String),
33
34    /// Transaction error
35    #[error("Transaction error: {0}")]
36    Transaction(String),
37
38    /// Pool error
39    #[error("Connection pool error: {0}")]
40    Pool(String),
41
42    /// Internal error
43    #[error("Internal error: {0}")]
44    Internal(#[from] anyhow::Error),
45}
46
47impl From<diesel::result::Error> for DatabaseError {
48    fn from(err: diesel::result::Error) -> Self {
49        match err {
50            diesel::result::Error::DatabaseError(kind, info) => match kind {
51                diesel::result::DatabaseErrorKind::UniqueViolation => Self::ConstraintViolation(
52                    format!("Unique constraint violation: {}", info.message()),
53                ),
54                diesel::result::DatabaseErrorKind::ForeignKeyViolation => {
55                    Self::ConstraintViolation(format!(
56                        "Foreign key constraint violation: {}",
57                        info.message()
58                    ))
59                },
60                diesel::result::DatabaseErrorKind::NotNullViolation => Self::ConstraintViolation(
61                    format!("Not null constraint violation: {}", info.message()),
62                ),
63                _ => Self::QueryExecution(format!("Database error: {}", info.message())),
64            },
65            diesel::result::Error::NotFound => Self::QueryExecution("Record not found".to_string()),
66            diesel::result::Error::RollbackTransaction => {
67                Self::Transaction("Transaction was rolled back".to_string())
68            },
69            diesel::result::Error::AlreadyInTransaction => {
70                Self::Transaction("Already in transaction".to_string())
71            },
72            diesel::result::Error::NotInTransaction => {
73                Self::Transaction("Not in transaction".to_string())
74            },
75            _ => Self::QueryExecution(format!("Diesel error: {err}")),
76        }
77    }
78}
79
80impl From<deadpool_diesel::PoolError> for DatabaseError {
81    fn from(err: deadpool_diesel::PoolError) -> Self {
82        Self::Pool(format!("Connection pool error: {err}"))
83    }
84}
85
86impl From<deadpool_diesel::InteractError> for DatabaseError {
87    fn from(err: deadpool_diesel::InteractError) -> Self {
88        Self::Connection(format!("Connection interaction error: {err}"))
89    }
90}
91
92impl From<diesel_migrations::MigrationError> for DatabaseError {
93    fn from(err: diesel_migrations::MigrationError) -> Self {
94        Self::Migration(format!("Migration error: {err}"))
95    }
96}