Add error type for database queries
@@ -22,6 +22,7 @@ use std::fmt;
pub enum DatabaseError {
ConnectionError(Box<dyn Error>),
MigrationError(Box<dyn Error>),
+ QueryError(Box<dyn Error>),
}
impl Error for DatabaseError {
@@ -29,6 +30,7 @@ impl Error for DatabaseError {
match self {
DatabaseError::ConnectionError(e) => Some(&**e),
DatabaseError::MigrationError(e) => Some(&**e),
+ DatabaseError::QueryError(e) => Some(&**e),
}
}
}
@@ -38,6 +40,7 @@ impl fmt::Display for DatabaseError {
match self {
DatabaseError::ConnectionError(e) => write!(f, "Unable to connect to database: {}", e),
DatabaseError::MigrationError(e) => write!(f, "Unable to migrate database: {}", e),
+ DatabaseError::QueryError(e) => write!(f, "Database query failed: {}", e),
}
}
}
@@ -53,3 +56,9 @@ impl From<diesel_migrations::RunMigrationsError> for DatabaseError {
DatabaseError::MigrationError(Box::new(err))
}
}
+
+impl From<diesel::result::Error> for DatabaseError {
+ fn from(err: diesel::result::Error) -> Self {
+ DatabaseError::QueryError(Box::new(err))
+ }
+}
