zlicenser_server/
error.rs1#[derive(Debug, thiserror::Error)]
2pub enum Error {
3 #[error("record not found")]
4 NotFound,
5 #[error("unique constraint violation: {0}")]
6 Conflict(String),
7 #[error("foreign key constraint violation: {0}")]
8 ForeignKeyViolation(String),
9 #[error("database error: {0}")]
10 Database(String),
11 #[error("migration error: {0}")]
12 Migration(String),
13 #[error("corrupt stored data: {0}")]
14 Corrupt(String),
15}
16
17#[cfg(feature = "storage-sqlite")]
18impl From<sqlx::Error> for Error {
19 fn from(e: sqlx::Error) -> Self {
20 match &e {
21 sqlx::Error::RowNotFound => Error::NotFound,
22 sqlx::Error::Database(db) => {
23 let msg = db.message().to_string();
24 if db.is_unique_violation() {
25 Error::Conflict(msg)
26 } else if db.is_foreign_key_violation() || msg.contains("FOREIGN KEY") {
27 Error::ForeignKeyViolation(msg)
28 } else {
29 Error::Database(msg)
30 }
31 }
32 _ => Error::Database(e.to_string()),
33 }
34 }
35}
36
37#[cfg(all(feature = "storage-postgres", not(feature = "storage-sqlite")))]
38impl From<sqlx::Error> for Error {
39 fn from(e: sqlx::Error) -> Self {
40 match &e {
41 sqlx::Error::RowNotFound => Error::NotFound,
42 sqlx::Error::Database(db) => {
43 let msg = db.message().to_string();
44 if db.is_unique_violation() {
45 Error::Conflict(msg)
46 } else if db.is_foreign_key_violation() {
47 Error::ForeignKeyViolation(msg)
48 } else {
49 Error::Database(msg)
50 }
51 }
52 _ => Error::Database(e.to_string()),
53 }
54 }
55}