rorm_db/
error.rs

1//! Error type to simplify propagating different error types.
2
3use std::{error, fmt};
4
5use sqlx::Error as SqlxError;
6
7use crate::row::OwnedRowError;
8
9/// Error type to simplify propagating different error types.
10#[derive(Debug)]
11pub enum Error {
12    /// Error returned from Sqlx
13    SqlxError(SqlxError),
14
15    /// Error for pointing to configuration errors.
16    ConfigurationError(String),
17
18    /// SQL building error
19    SQLBuildError(rorm_sql::error::Error),
20
21    /// Error returned by [`Row::get`](crate::Row::get)
22    RowError(OwnedRowError),
23}
24
25impl error::Error for Error {
26    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
27        match self {
28            Error::SqlxError(source) => Some(source),
29            Error::ConfigurationError(_) => None,
30            Error::SQLBuildError(source) => Some(source),
31            Error::RowError(source) => Some(source),
32        }
33    }
34}
35
36impl fmt::Display for Error {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        match self {
39            Error::SqlxError(error) => write!(f, "sqlx error: {error}"),
40            Error::ConfigurationError(error) => write!(f, "configuration error: {error}",),
41            Error::SQLBuildError(error) => {
42                write!(f, "sql error: {error}")
43            }
44            Error::RowError(error) => {
45                write!(f, "{error}")
46            }
47        }
48    }
49}
50
51impl From<SqlxError> for Error {
52    fn from(source: SqlxError) -> Self {
53        Error::SqlxError(source)
54    }
55}
56
57impl From<rorm_sql::error::Error> for Error {
58    fn from(source: rorm_sql::error::Error) -> Self {
59        Error::SQLBuildError(source)
60    }
61}