Skip to main content

rullst_orm/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// The standard error type for rullst-orm, shielding users from internal dependency errors.
5#[derive(Debug)]
6pub enum RullstError {
7    /// A record was not found in the database.
8    RecordNotFound,
9    /// A general database or query error.
10    DatabaseError(String),
11    /// A serialization or deserialization error (e.g., JSON).
12    SerializationError(String),
13    /// A cache or event-related error.
14    CacheError(String),
15    /// Other internal errors.
16    Internal(String),
17}
18
19impl fmt::Display for RullstError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            RullstError::RecordNotFound => write!(f, "Record not found"),
23            RullstError::DatabaseError(msg) => write!(f, "Database error: {}", msg),
24            RullstError::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
25            RullstError::CacheError(msg) => write!(f, "Cache error: {}", msg),
26            RullstError::Internal(msg) => write!(f, "Internal error: {}", msg),
27        }
28    }
29}
30
31impl Error for RullstError {}
32
33impl From<sqlx::Error> for RullstError {
34    fn from(err: sqlx::Error) -> Self {
35        match err {
36            sqlx::Error::RowNotFound => RullstError::RecordNotFound,
37            _ => RullstError::DatabaseError(err.to_string()),
38        }
39    }
40}
41
42impl From<serde_json::Error> for RullstError {
43    fn from(err: serde_json::Error) -> Self {
44        RullstError::SerializationError(err.to_string())
45    }
46}
47
48#[cfg(feature = "redis")]
49impl From<redis::RedisError> for RullstError {
50    fn from(err: redis::RedisError) -> Self {
51        RullstError::CacheError(err.to_string())
52    }
53}