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>;