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
//! Error type to simplify propagating different error types.

use std::{error, fmt};

#[cfg(feature = "sqlx")]
use sqlx::Error as SqlxError;

#[cfg(not(feature = "sqlx"))]
const _: () = {
    /// Represent all ways a method can fail within SQLx.
    ///
    /// I.e. not at all since sqlx isn't a dependency.
    #[derive(Debug)]
    pub enum SqlxError {}

    impl error::Error for Error {}

    impl fmt::Display for Error {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            unreachable!("You shouldn't be able to get an instance of an empty enum!");
        }
    }
};

/// Error type to simplify propagating different error types.
#[derive(Debug)]
pub enum Error {
    /// Error returned from Sqlx
    SqlxError(SqlxError),

    /// Error for pointing to configuration errors.
    ConfigurationError(String),

    /// DecodeError
    DecodeError(String),

    /// SQL building error
    SQLBuildError(rorm_sql::error::Error),
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::SqlxError(source) => Some(source),
            Error::ConfigurationError(_) => None,
            Error::DecodeError(_) => None,
            Error::SQLBuildError(source) => Some(source),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::SqlxError(error) => write!(f, "sqlx error: {error}"),
            Error::ConfigurationError(error) => write!(f, "configuration error: {error}",),
            Error::DecodeError(error) => {
                write!(f, "decode error: {error}")
            }
            Error::SQLBuildError(error) => {
                write!(f, "sql error: {error}")
            }
        }
    }
}

impl From<SqlxError> for Error {
    fn from(source: SqlxError) -> Self {
        Error::SqlxError(source)
    }
}

impl From<rorm_sql::error::Error> for Error {
    fn from(source: rorm_sql::error::Error) -> Self {
        Error::SQLBuildError(source)
    }
}