orbis_plugin_api/sdk/
error.rs

1//! Plugin SDK error types.
2
3use std::fmt;
4
5/// Result type for plugin operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error type for plugin operations
9#[derive(Debug)]
10pub enum Error {
11    /// JSON serialization/deserialization error
12    Json(serde_json::Error),
13
14    /// State operation error
15    State(String),
16
17    /// Database operation error
18    Database(String),
19
20    /// HTTP request error
21    Http(String),
22
23    /// Permission denied
24    PermissionDenied(String),
25
26    /// Invalid input
27    InvalidInput(String),
28
29    /// Not found
30    NotFound(String),
31
32    /// Internal plugin error
33    Internal(String),
34
35    /// Validation error
36    Validation(String),
37
38    /// Timeout error
39    Timeout(String),
40}
41
42impl fmt::Display for Error {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            Self::Json(e) => write!(f, "JSON error: {}", e),
46            Self::State(msg) => write!(f, "State error: {}", msg),
47            Self::Database(msg) => write!(f, "Database error: {}", msg),
48            Self::Http(msg) => write!(f, "HTTP error: {}", msg),
49            Self::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
50            Self::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
51            Self::NotFound(msg) => write!(f, "Not found: {}", msg),
52            Self::Internal(msg) => write!(f, "Internal error: {}", msg),
53            Self::Validation(msg) => write!(f, "Validation error: {}", msg),
54            Self::Timeout(msg) => write!(f, "Timeout: {}", msg),
55        }
56    }
57}
58
59impl std::error::Error for Error {
60    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61        match self {
62            Self::Json(e) => Some(e),
63            _ => None,
64        }
65    }
66}
67
68impl From<serde_json::Error> for Error {
69    fn from(e: serde_json::Error) -> Self {
70        Self::Json(e)
71    }
72}
73
74impl From<std::string::FromUtf8Error> for Error {
75    fn from(e: std::string::FromUtf8Error) -> Self {
76        Self::InvalidInput(format!("Invalid UTF-8: {}", e))
77    }
78}
79
80impl Error {
81    /// Create a state error
82    #[inline]
83    pub fn state<S: Into<String>>(msg: S) -> Self {
84        Self::State(msg.into())
85    }
86
87    /// Create a database error
88    #[inline]
89    pub fn database<S: Into<String>>(msg: S) -> Self {
90        Self::Database(msg.into())
91    }
92
93    /// Create an HTTP error
94    #[inline]
95    pub fn http<S: Into<String>>(msg: S) -> Self {
96        Self::Http(msg.into())
97    }
98
99    /// Create a permission denied error
100    #[inline]
101    pub fn permission_denied<S: Into<String>>(msg: S) -> Self {
102        Self::PermissionDenied(msg.into())
103    }
104
105    /// Create an invalid input error
106    #[inline]
107    pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
108        Self::InvalidInput(msg.into())
109    }
110
111    /// Create a not found error
112    #[inline]
113    pub fn not_found<S: Into<String>>(msg: S) -> Self {
114        Self::NotFound(msg.into())
115    }
116
117    /// Create an internal error
118    #[inline]
119    pub fn internal<S: Into<String>>(msg: S) -> Self {
120        Self::Internal(msg.into())
121    }
122
123    /// Create a validation error
124    #[inline]
125    pub fn validation<S: Into<String>>(msg: S) -> Self {
126        Self::Validation(msg.into())
127    }
128
129    /// Get HTTP status code for this error
130    #[must_use]
131    pub const fn status_code(&self) -> u16 {
132        match self {
133            Self::Json(_) | Self::InvalidInput(_) | Self::Validation(_) => 400,
134            Self::PermissionDenied(_) => 403,
135            Self::NotFound(_) => 404,
136            Self::Timeout(_) => 408,
137            Self::State(_) | Self::Database(_) | Self::Http(_) | Self::Internal(_) => 500,
138        }
139    }
140}