1use std::error::Error;
2use std::fmt;
3
4#[derive(Debug)]
6pub enum RullstError {
7 RecordNotFound,
9 DatabaseError(String),
11 SerializationError(String),
13 CacheError(String),
15 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}