fish_lib/game/errors/
database.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum GameDatabaseError {
5    #[error("Database connection failed: {msg}")]
6    ConnectionFailed { msg: String },
7    #[error("Database foreign key violation: {msg}")]
8    ForeignKeyViolation { msg: String },
9    #[error("Database migrations failed: {msg}")]
10    MigrationsFailed { msg: String },
11    #[error("No database connection specified")]
12    MissingConnection,
13    #[error("Record not found")]
14    NotFound,
15    #[error("Database error: {msg}")]
16    Other { msg: String },
17    #[error("Database unique constraint violation: {msg}")]
18    UniqueConstraintViolation { msg: String },
19}
20
21impl GameDatabaseError {
22    pub fn connection_failed(msg: &str) -> Self {
23        Self::ConnectionFailed {
24            msg: msg.to_string(),
25        }
26    }
27
28    pub fn foreign_key_violation(msg: &str) -> Self {
29        Self::ForeignKeyViolation {
30            msg: msg.to_string(),
31        }
32    }
33
34    pub fn migrations_failed(msg: &str) -> Self {
35        Self::MigrationsFailed {
36            msg: msg.to_string(),
37        }
38    }
39
40    pub fn missing_connection() -> Self {
41        Self::MissingConnection
42    }
43
44    pub fn not_found() -> Self {
45        Self::NotFound
46    }
47
48    pub fn other(msg: &str) -> Self {
49        Self::Other {
50            msg: msg.to_string(),
51        }
52    }
53
54    pub fn unique_constraint_violation(msg: &str) -> Self {
55        Self::UniqueConstraintViolation {
56            msg: msg.to_string(),
57        }
58    }
59
60    pub fn is_connection_failed(&self) -> bool {
61        matches!(self, Self::ConnectionFailed { .. })
62    }
63
64    pub fn is_foreign_key_violation(&self) -> bool {
65        matches!(self, Self::ForeignKeyViolation { .. })
66    }
67
68    pub fn is_migrations_failed(&self) -> bool {
69        matches!(self, Self::MigrationsFailed { .. })
70    }
71
72    pub fn is_missing_connection(&self) -> bool {
73        matches!(self, Self::MissingConnection)
74    }
75
76    pub fn is_not_found(&self) -> bool {
77        matches!(self, Self::NotFound)
78    }
79
80    pub fn is_other(&self) -> bool {
81        matches!(self, Self::Other { .. })
82    }
83
84    pub fn is_unique_constraint_violation(&self) -> bool {
85        matches!(self, Self::UniqueConstraintViolation { .. })
86    }
87}