miden_note_transport_node/database/
error.rs1use thiserror::Error;
2
3#[derive(Debug, Error)]
5pub enum DatabaseError {
6 #[error("Configuration error: {0}")]
8 Configuration(String),
9
10 #[error("Connection error: {0}")]
12 Connection(String),
13
14 #[error("Migration error: {0}")]
16 Migration(String),
17
18 #[error("Query execution error: {0}")]
20 QueryExecution(String),
21
22 #[error("Serialization error: {0}")]
24 Serialization(String),
25
26 #[error("Deserialization error: {0}")]
28 Deserialization(String),
29
30 #[error("Constraint violation: {0}")]
32 ConstraintViolation(String),
33
34 #[error("Transaction error: {0}")]
36 Transaction(String),
37
38 #[error("Connection pool error: {0}")]
40 Pool(String),
41
42 #[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}