Skip to main content

welds_connections/errors/
mod.rs

1use std::fmt::Display;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug)]
6pub enum Error {
7    #[cfg(any(feature = "mysql", feature = "sqlite", feature = "postgres"))]
8    Sqlx(sqlx::Error),
9    #[cfg(feature = "mssql")]
10    TiberiusConnPool(std::io::Error),
11    #[cfg(feature = "mssql")]
12    Tiberius(tiberius::error::Error),
13    #[cfg(feature = "sqlite-sync")]
14    Rusqlite(rusqlite::Error),
15    InvalidDatabaseUrl,
16    RowNowFound,
17    PoolError,
18    ClosedTransaction,
19    ColumnNotFound(String),
20    UnexpectedNoneInColumn(String),
21    JsonParseError(String, String),
22}
23
24impl std::error::Error for Error {}
25
26impl Display for Error {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        let message = match self {
29            #[cfg(any(feature = "mysql", feature = "sqlite", feature = "postgres"))]
30            Error::Sqlx(err) => err.to_string(),
31            #[cfg(feature = "sqlite-sync")]
32            Error::Rusqlite(err) => err.to_string(),
33            #[cfg(feature = "mssql")]
34            Error::TiberiusConnPool(err) => err.to_string(),
35            #[cfg(feature = "mssql")]
36            Error::Tiberius(err) => err.to_string(),
37            Error::InvalidDatabaseUrl => "Invalid database URL. If your connection string is valid, make sure the feature for your database type is enabled".to_string(),
38            Error::PoolError => "the MSSQL connection pool has a locked mutex".to_string(),
39            Error::RowNowFound => "Row not found".to_string(),
40            Error::ClosedTransaction => {
41                "SQL can not be executed on a closed transaction".to_string()
42            }
43            Error::ColumnNotFound(name) => format!("Column not found: {name}"),
44            Error::UnexpectedNoneInColumn(name) => format!("Unexpected None in column: {name}"),
45            Error::JsonParseError(col, json) => {
46                format!("unable to parse json in column: {col}. json: {json}")
47            }
48        };
49
50        f.write_str(&message)?;
51        Ok(())
52    }
53}
54
55#[cfg(any(feature = "mysql", feature = "sqlite", feature = "postgres"))]
56impl From<sqlx::error::Error> for Error {
57    fn from(inner: sqlx::error::Error) -> Self {
58        Error::Sqlx(inner)
59    }
60}
61
62//  #[cfg(feature = "mssql")]
63//  impl From<bb8_tiberius::Error> for Error {
64//      fn from(inner: bb8_tiberius::Error) -> Self {
65//          Error::TiberiusConnPool(inner)
66//      }
67//  }
68//
69//  #[cfg(feature = "mssql")]
70//  impl<T> From<bb8::RunError<T>> for Error {
71//      fn from(inner: bb8::RunError<T>) -> Self {
72//          let inner = match inner {
73//              bb8::RunError::TimedOut => "bb8 timeout",
74//              bb8::RunError::User(_) => "bb8 user error",
75//          };
76//          Error::Bb8(inner)
77//      }
78//  }
79
80#[cfg(feature = "mssql")]
81impl From<tiberius::error::Error> for Error {
82    fn from(inner: tiberius::error::Error) -> Self {
83        Error::Tiberius(inner)
84    }
85}
86
87#[cfg(feature = "sqlite-sync")]
88impl From<rusqlite::Error> for Error {
89    fn from(inner: rusqlite::Error) -> Self {
90        Error::Rusqlite(inner)
91    }
92}