ts_sql_helper_lib/
error.rs1pub trait SqlError: Sized {
3 fn fk_violation<E, F: FnOnce() -> E>(self, f: F) -> Result<Self, E>;
5 fn unique_violation<E, F: FnOnce() -> E>(self, f: F) -> Result<Self, E>;
7}
8
9impl<T> SqlError for Result<T, postgres::Error> {
10 fn fk_violation<E, F: FnOnce() -> E>(self, f: F) -> Result<Self, E> {
11 if let Err(error) = &self {
12 if let Some(sql_error) = error.code() {
13 if sql_error == &postgres::error::SqlState::FOREIGN_KEY_VIOLATION {
14 return Err(f());
15 }
16 }
17 }
18
19 Ok(self)
20 }
21
22 fn unique_violation<E, F: FnOnce() -> E>(self, f: F) -> Result<Self, E> {
23 if let Err(error) = &self {
24 if let Some(sql_error) = error.code() {
25 if sql_error == &postgres::error::SqlState::UNIQUE_VIOLATION {
26 return Err(f());
27 }
28 }
29 }
30
31 Ok(self)
32 }
33}