modio_logger_db/
error.rs

1// Author: D.S. Ljungmark <spider@skuggor.se>, Modio AB
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6    #[error("SQL Reported error")]
7    SQLx { source: sqlx::Error },
8    #[error("Migration error")]
9    Migration {
10        #[from]
11        source: sqlx::migrate::MigrateError,
12    },
13    #[error("Unique constraint failed")]
14    Unique { source: sqlx::Error },
15    #[error("Database Pool Timeout")]
16    Timeout { source: sqlx::Error },
17    #[error("Not found")]
18    NotFound { source: sqlx::Error },
19    #[error("Serialization failed")]
20    Serde(#[from] serde_json::Error),
21}
22
23impl std::convert::From<sqlx::Error> for Error {
24    fn from(source: sqlx::Error) -> Self {
25        match source {
26            sqlx::Error::RowNotFound => Self::NotFound { source },
27            sqlx::Error::PoolTimedOut => Self::Timeout { source },
28            _ => {
29                if source.to_string().contains("UNIQUE constraint failed") {
30                    Self::Unique { source }
31                } else {
32                    Self::SQLx { source }
33                }
34            }
35        }
36    }
37}