1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum VtxError {
10 DatabaseError(String),
12
13 SerializationError(String),
15
16 AuthDenied(u16),
18
19 PermissionDenied(String),
21
22 NotFound(String),
24
25 Internal(String),
27}
28
29impl VtxError {
30 pub fn from_host_message(message: impl Into<String>) -> Self {
32 let msg = message.into();
33 let lower = msg.to_lowercase();
34
35 if lower.contains("permission denied") {
36 return VtxError::PermissionDenied(msg);
37 }
38
39 if lower.contains("uuid not found") || lower.contains("not found") {
40 return VtxError::NotFound(msg);
41 }
42
43 VtxError::Internal(msg)
44 }
45}
46
47impl fmt::Display for VtxError {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 match self {
50 VtxError::DatabaseError(msg) => write!(f, "Database error: {}", msg),
51 VtxError::SerializationError(msg) => write!(f, "Data serialization error: {}", msg),
52 VtxError::AuthDenied(code) => write!(f, "Authentication denied (Code: {})", code),
53 VtxError::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
54 VtxError::NotFound(msg) => write!(f, "Resource not found: {}", msg),
55 VtxError::Internal(msg) => write!(f, "Internal error: {}", msg),
56 }
57 }
58}
59
60impl std::error::Error for VtxError {}
61
62pub type VtxResult<T> = Result<T, VtxError>;
66
67#[cfg(test)]
68mod tests {
69 use super::VtxError;
70
71 #[test]
72 fn from_host_message_maps_permission_denied() {
73 let err = VtxError::from_host_message("Permission denied: read only");
74 match err {
75 VtxError::PermissionDenied(msg) => {
76 assert!(msg.contains("Permission denied"));
77 }
78 _ => panic!("expected PermissionDenied"),
79 }
80 }
81
82 #[test]
83 fn from_host_message_maps_not_found() {
84 let err = VtxError::from_host_message("UUID not found: 1234");
85 match err {
86 VtxError::NotFound(msg) => {
87 assert!(msg.contains("not found"));
88 }
89 _ => panic!("expected NotFound"),
90 }
91 }
92
93 #[test]
94 fn from_host_message_defaults_to_internal() {
95 let err = VtxError::from_host_message("something unexpected");
96 match err {
97 VtxError::Internal(msg) => {
98 assert!(msg.contains("unexpected"));
99 }
100 _ => panic!("expected Internal"),
101 }
102 }
103
104 #[test]
105 fn display_formats_errors() {
106 let err = VtxError::AuthDenied(401);
107 assert_eq!(err.to_string(), "Authentication denied (Code: 401)");
108
109 let err = VtxError::DatabaseError("boom".to_string());
110 assert_eq!(err.to_string(), "Database error: boom");
111 }
112}