1use sqlx::error::DatabaseError;
2
3use crate::DbErr;
4
5pub trait DatabaseErrorExt {
8 fn is_error_code(&self, error_code: &str) -> bool;
9
10 fn is_database_does_not_exist(&self) -> bool;
11
12 fn is_database_exists(&self) -> bool;
13
14 fn is_table_does_not_exist(&self) -> bool;
15
16 fn is_duplicate_record(&self) -> bool;
17}
18
19impl DatabaseErrorExt for &dyn DatabaseError {
20 fn is_error_code(&self, error_code: &str) -> bool {
21 self.code()
22 .is_some_and(|code| code.to_string().eq(error_code))
23 }
24
25 fn is_database_does_not_exist(&self) -> bool {
26 self.is_error_code("3D000" )
27 }
28
29 fn is_database_exists(&self) -> bool {
30 self.is_error_code("42P04" )
31 }
32
33 fn is_table_does_not_exist(&self) -> bool {
34 self.is_error_code("42P01" )
35 }
36
37 fn is_duplicate_record(&self) -> bool {
38 self.is_unique_violation()
39 }
40}
41
42impl DatabaseErrorExt for DbErr {
43 fn is_error_code(&self, error_code: &str) -> bool {
44 self.as_database_error()
45 .is_some_and(|error| error.is_error_code(error_code))
46 }
47
48 fn is_database_does_not_exist(&self) -> bool {
49 self.as_database_error()
50 .is_some_and(|error| error.is_database_does_not_exist())
51 }
52
53 fn is_database_exists(&self) -> bool {
54 self.as_database_error()
55 .is_some_and(|error| error.is_database_exists())
56 }
57
58 fn is_table_does_not_exist(&self) -> bool {
59 self.as_database_error()
60 .is_some_and(|error| error.is_table_does_not_exist())
61 }
62
63 fn is_duplicate_record(&self) -> bool {
64 self.as_database_error()
65 .is_some_and(|error| error.is_duplicate_record())
66 }
67}