1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use cfg_if::cfg_if;
use r2d2;
use thiserror::Error;
use url;

cfg_if! {if #[cfg(feature = "with-postgres")]{
    use crate::pg::PostgresError;
}}

cfg_if! {if #[cfg(feature = "with-sqlite")]{
    use crate::sqlite::SqliteError;
    use rusqlite;
}}

cfg_if! {if #[cfg(feature = "with-mysql")]{
    use crate::my::MysqlError;
}}

#[derive(Debug, Error)]
pub enum ConnectError {
    #[error("No such pool connection")]
    NoSuchPoolConnection,
    #[error("{0}")]
    ParseError(#[from] ParseError),
    #[error("Database not supported: {0}")]
    UnsupportedDb(String),
    #[error("{0}")]
    R2d2Error(#[from] r2d2::Error),
}

#[derive(Debug, Error)]
pub enum ParseError {
    #[error("Database url parse error: {0}")]
    DbUrlParseError(#[from] url::ParseError),
}

#[derive(Debug, Error)]
#[error("{0}")]
pub enum PlatformError {
    #[cfg(feature = "with-postgres")]
    #[error("{0}")]
    PostgresError(#[from] PostgresError),
    #[cfg(feature = "with-sqlite")]
    #[error("{0}")]
    SqliteError(#[from] SqliteError),
    #[cfg(feature = "with-mysql")]
    #[error("{0}")]
    MysqlError(#[from] MysqlError),
}

impl From<PlatformError> for DataOpError {
    /// attempt to convert platform specific error to DataOpeation error
    fn from(platform_error: PlatformError) -> Self {
        match platform_error {
            #[cfg(feature = "with-postgres")]
            PlatformError::PostgresError(postgres_err) => {
                match postgres_err {
                    PostgresError::Sql(ref pg_err, ref sql) => {
                        if let Some(db_err) = pg_err.as_db_error() {
                            use crate::TableName;

                            DataOpError::ConstraintError {
                                severity: db_err.severity().to_owned(),
                                code: db_err.code().code().to_string(),
                                message: db_err.message().to_owned(),
                                detail: db_err.detail().map(String::from),
                                cause_table: db_err.table().map(|table| {
                                    TableName {
                                        name: table.to_string(),
                                        schema: db_err.schema().map(String::from),
                                        alias: None,
                                    }
                                    .complete_name()
                                }),
                                constraint: db_err.constraint().map(String::from),
                                column: db_err.column().map(String::from),
                                datatype: db_err.datatype().map(String::from),
                                sql: sql.to_owned(),
                            }
                        } else {
                            DataOpError::GenericError {
                                message: postgres_err.to_string(),
                                sql: None,
                            }
                        }
                    }
                    _ => {
                        DataOpError::GenericError {
                            message: postgres_err.to_string(),
                            sql: None,
                        }
                    }
                }
            }
            #[cfg(feature = "with-sqlite")]
            PlatformError::SqliteError(e) => {
                DataOpError::GenericError {
                    message: e.to_string(),
                    sql: None,
                }
            }
            #[cfg(feature = "with-mysql")]
            PlatformError::MysqlError(e) => {
                DataOpError::GenericError {
                    message: e.to_string(),
                    sql: None,
                }
            }
        }
    }
}

//Note: this is needed coz there is 2 level of variant before we can convert postgres error to
//platform error
#[cfg(feature = "with-postgres")]
impl From<PostgresError> for DbError {
    fn from(e: PostgresError) -> Self { DbError::DataOpError(PlatformError::from(e).into()) }
}

#[cfg(feature = "with-sqlite")]
impl From<rusqlite::Error> for DbError {
    fn from(e: rusqlite::Error) -> Self {
        DbError::DataOpError(PlatformError::SqliteError(SqliteError::from(e)).into())
    }
}

#[cfg(feature = "with-sqlite")]
impl From<SqliteError> for DbError {
    fn from(e: SqliteError) -> Self { DbError::DataOpError(PlatformError::SqliteError(e).into()) }
}

#[cfg(feature = "with-mysql")]
impl From<MysqlError> for DbError {
    fn from(e: MysqlError) -> Self { DbError::DataOpError(PlatformError::MysqlError(e).into()) }
}

#[derive(Debug, Error)]
pub enum DbError {
    #[error("Sql injection attempt error: {0}")]
    SqlInjectionAttempt(String),
    #[error("{0}")]
    DataError(#[from] DataError),
    #[error("{0}")]
    DataOpError(#[from] DataOpError),
    #[error("{0}")]
    ConvertError(#[from] ConvertError),
    #[error("{0}")]
    ConnectError(#[from] ConnectError), //agnostic connection error
    #[error("Unsupported operation: {0}")]
    UnsupportedOperation(String),
}

#[derive(Debug, Error)]
pub enum DataOpError {
    /// The Data Delete Operation failed due record is still referenced from another table
    #[error("{constraint:?}, {cause_table:?}")]
    ConstraintError {
        severity: String,
        code: String,
        message: String,
        detail: Option<String>,
        cause_table: Option<String>,
        constraint: Option<String>,
        column: Option<String>,
        datatype: Option<String>,
        sql: String,
    },
    #[error("{message}")]
    GenericError {
        message: String,
        sql: Option<String>,
    },
}

#[derive(Debug, Error)]
pub enum ConvertError {
    #[error("Unknown data type")]
    UnknownDataType,
    #[error("Unsupported data type {0}")]
    UnsupportedDataType(String),
}

#[derive(Debug, Error)]
pub enum DataError {
    #[error("Zero record returned")]
    ZeroRecordReturned,
    #[error("More than one record returned")]
    MoreThan1RecordReturned,
    #[error("Table {0} not found")]
    TableNameNotFound(String),
}