hypercall/snapshot/
error.rs1use std::fmt;
4
5#[derive(Debug)]
7pub enum SnapshotError {
8 DbError(String),
10 Serialization(String),
12 NotFound(i64),
14 InvalidState(String),
16 ServiceError(String),
18}
19
20impl fmt::Display for SnapshotError {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 match self {
23 SnapshotError::DbError(msg) => write!(f, "Database error: {}", msg),
24 SnapshotError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
25 SnapshotError::NotFound(id) => write!(f, "Snapshot not found: {}", id),
26 SnapshotError::InvalidState(msg) => write!(f, "Invalid state: {}", msg),
27 SnapshotError::ServiceError(msg) => write!(f, "Service error: {}", msg),
28 }
29 }
30}
31
32impl std::error::Error for SnapshotError {}
33
34impl From<bincode::Error> for SnapshotError {
35 fn from(e: bincode::Error) -> Self {
36 SnapshotError::Serialization(e.to_string())
37 }
38}
39
40impl From<diesel::result::Error> for SnapshotError {
41 fn from(e: diesel::result::Error) -> Self {
42 SnapshotError::DbError(e.to_string())
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_error_display() {
52 let err = SnapshotError::DbError("connection failed".to_string());
53 assert!(err.to_string().contains("Database error"));
54
55 let err = SnapshotError::NotFound(42);
56 assert!(err.to_string().contains("42"));
57 }
58}