Skip to main content

hypercall/snapshot/
error.rs

1//! Snapshot error types.
2
3use std::fmt;
4
5/// Errors that can occur during snapshot operations.
6#[derive(Debug)]
7pub enum SnapshotError {
8    /// Database operation failed
9    DbError(String),
10    /// Serialization or deserialization failed
11    Serialization(String),
12    /// Snapshot not found
13    NotFound(i64),
14    /// Invalid snapshot state
15    InvalidState(String),
16    /// Service error (e.g., from Snapshotable implementation)
17    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}