1#[derive(Debug)]
2pub enum DatabaseError {
3 KeyNotFound,
4 UserNotFound,
5 UserAlreadyExists,
6 RoleNotFound,
7 RoleAlreadyExists,
8 Forbidden,
9 Utf8Error,
10 EncryptionError,
11 IoError(std::io::Error),
12 SQLError(sqlx::Error),
13 Argon2Error(argon2::password_hash::Error)
14}
15
16impl From<std::str::Utf8Error> for DatabaseError {
17 fn from(_error: std::str::Utf8Error) -> Self {
18 Self::Utf8Error
19 }
20}
21
22impl From<sqlx::Error> for DatabaseError {
23 fn from(err: sqlx::Error) -> Self {
24 Self::SQLError(err)
25 }
26}
27
28impl From<argon2::password_hash::Error> for DatabaseError {
29 fn from(err: argon2::password_hash::Error) -> Self {
30 Self::Argon2Error(err)
31 }
32}
33
34impl std::fmt::Display for DatabaseError {
35 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36 match self {
37 Self::KeyNotFound => write!(f, "Key wasn't found"),
38 Self::UserNotFound => write!(f, "User wasn't found"),
39 Self::UserAlreadyExists => write!(f, "User already exists"),
40 Self::RoleNotFound => write!(
41 f,
42 "Attempted to delete a role that wasn't found for a given user"
43 ),
44 Self::RoleAlreadyExists => {
45 write!(f, "Attempted to add role that already existed for user")
46 }
47 Self::Forbidden => write!(
48 f,
49 "User attempted to access a key that they don't have access to"
50 ),
51 Self::Utf8Error => write!(f, "Error while trying to convert bytes to UTF8 string"),
52 Self::EncryptionError => write!(f, "Error while trying to encrypt or decrypt a value"),
53 Self::SQLError(e) => write!(f, "SQL error: {e}"),
54 Self::IoError(e) => write!(f, "IO error: {e}"),
55 Self::Argon2Error(e) => write!(f, "Hashing error: {e}"),
56 }
57 }
58}
59
60impl std::error::Error for DatabaseError {}