vtx_sdk/
vtx_error.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Plugin runtime error type (unified error model).
5///
6/// Defines all known errors that may occur during plugin execution.
7/// All errors support serialization and can be used for HTTP responses or log transmission.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum VtxError {
10    /// Underlying database error (e.g., SQL execution failure, constraint conflicts).
11    DatabaseError(String),
12
13    /// Serialization or deserialization failure (e.g., JSON format mismatch).
14    SerializationError(String),
15
16    /// Authentication failed (includes suggested HTTP status code, e.g., 401 / 403).
17    AuthDenied(u16),
18
19    /// Insufficient permissions (e.g., attempting a write operation in a read-only environment).
20    PermissionDenied(String),
21
22    /// Resource not found (e.g., file, video, or user not found).
23    NotFound(String),
24
25    /// Internal plugin logic error (fallback type).
26    Internal(String),
27}
28
29impl VtxError {
30    /// Maps the `string` error message returned from the host side to a reasonably classified error type.
31    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
62/// Plugin standard result type alias.
63///
64/// Recommended for use in all interfaces returning `VtxError` to ensure a unified error chain.
65pub 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}