turbovault_plugin_api/
error.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6#[non_exhaustive]
7pub enum PluginErrorCode {
8 InvalidInput,
10 NotFound,
12 Conflict,
14 PermissionDenied,
20 Unavailable,
22 Timeout,
24 Internal,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
30#[error("{code:?}: {message}")]
31pub struct PluginError {
32 pub code: PluginErrorCode,
34 pub message: String,
36}
37
38impl PluginError {
39 pub fn new(code: PluginErrorCode, message: impl Into<String>) -> Self {
41 Self {
42 code,
43 message: message.into(),
44 }
45 }
46
47 pub fn invalid_input(message: impl Into<String>) -> Self {
49 Self::new(PluginErrorCode::InvalidInput, message)
50 }
51
52 pub fn not_found(message: impl Into<String>) -> Self {
54 Self::new(PluginErrorCode::NotFound, message)
55 }
56
57 pub fn conflict(message: impl Into<String>) -> Self {
59 Self::new(PluginErrorCode::Conflict, message)
60 }
61
62 pub fn permission_denied(message: impl Into<String>) -> Self {
64 Self::new(PluginErrorCode::PermissionDenied, message)
65 }
66
67 pub fn unavailable(message: impl Into<String>) -> Self {
69 Self::new(PluginErrorCode::Unavailable, message)
70 }
71
72 pub fn timeout(message: impl Into<String>) -> Self {
74 Self::new(PluginErrorCode::Timeout, message)
75 }
76
77 pub fn internal(message: impl Into<String>) -> Self {
79 Self::new(PluginErrorCode::Internal, message)
80 }
81}
82
83pub type PluginResult<T> = Result<T, PluginError>;