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, Clone)]
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    /// A validation error, such as invalid SQL identifiers.
16    Validation(String),
17    /// Other internal errors.
18    Internal(String),
19}
20
21impl fmt::Display for RullstError {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            RullstError::RecordNotFound => write!(f, "Record not found"),
25            RullstError::DatabaseError(msg) => write!(f, "Database error: {}", msg),
26            RullstError::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
27            RullstError::CacheError(msg) => write!(f, "Cache error: {}", msg),
28            RullstError::Validation(msg) => write!(f, "Validation error: {}", msg),
29            RullstError::Internal(msg) => write!(f, "Internal error: {}", msg),
30        }
31    }
32}
33
34impl Error for RullstError {}
35
36impl From<sqlx::Error> for RullstError {
37    fn from(err: sqlx::Error) -> Self {
38        match err {
39            sqlx::Error::RowNotFound => RullstError::RecordNotFound,
40            _ => RullstError::DatabaseError(err.to_string()),
41        }
42    }
43}
44
45impl From<serde_json::Error> for RullstError {
46    fn from(err: serde_json::Error) -> Self {
47        RullstError::SerializationError(err.to_string())
48    }
49}
50
51#[cfg(feature = "redis")]
52impl From<redis::RedisError> for RullstError {
53    fn from(err: redis::RedisError) -> Self {
54        RullstError::CacheError(err.to_string())
55    }
56}