1use thiserror::Error;
4
5pub type PluginResult<T> = Result<T, PluginError>;
7
8#[derive(Error, Debug)]
10pub enum PluginError {
11 #[error("invalid lifecycle state: expected {expected}, got {actual}")]
13 InvalidState { expected: String, actual: String },
14
15 #[error("initialization failed: {0}")]
17 InitializationFailed(String),
18
19 #[error("shutdown failed: {0}")]
21 ShutdownFailed(String),
22
23 #[error("configuration error: {0}")]
25 ConfigError(String),
26
27 #[error("serialization error: {0}")]
29 SerializationError(String),
30
31 #[error("unknown message type: {0}")]
33 UnknownMessageType(String),
34
35 #[error("handler error: {0}")]
37 HandlerError(String),
38
39 #[error("runtime error: {0}")]
41 RuntimeError(String),
42
43 #[error("request cancelled")]
45 Cancelled,
46
47 #[error("request timed out")]
49 Timeout,
50
51 #[error("internal error: {0}")]
53 Internal(String),
54
55 #[error("FFI error: {0}")]
57 FfiError(String),
58
59 #[error("too many concurrent requests (limit exceeded)")]
61 TooManyRequests,
62}
63
64impl PluginError {
65 pub fn error_code(&self) -> u32 {
67 match self {
68 PluginError::InvalidState { .. } => 1,
69 PluginError::InitializationFailed(_) => 2,
70 PluginError::ShutdownFailed(_) => 3,
71 PluginError::ConfigError(_) => 4,
72 PluginError::SerializationError(_) => 5,
73 PluginError::UnknownMessageType(_) => 6,
74 PluginError::HandlerError(_) => 7,
75 PluginError::RuntimeError(_) => 8,
76 PluginError::Cancelled => 9,
77 PluginError::Timeout => 10,
78 PluginError::Internal(_) => 11,
79 PluginError::FfiError(_) => 12,
80 PluginError::TooManyRequests => 13,
81 }
82 }
83
84 pub fn from_code(code: u32, message: String) -> Self {
86 match code {
87 1 => PluginError::InvalidState {
88 expected: String::new(),
89 actual: message,
90 },
91 2 => PluginError::InitializationFailed(message),
92 3 => PluginError::ShutdownFailed(message),
93 4 => PluginError::ConfigError(message),
94 5 => PluginError::SerializationError(message),
95 6 => PluginError::UnknownMessageType(message),
96 7 => PluginError::HandlerError(message),
97 8 => PluginError::RuntimeError(message),
98 9 => PluginError::Cancelled,
99 10 => PluginError::Timeout,
100 11 => PluginError::Internal(message),
101 12 => PluginError::FfiError(message),
102 13 => PluginError::TooManyRequests,
103 _ => PluginError::Internal(message),
104 }
105 }
106}
107
108impl From<serde_json::Error> for PluginError {
109 fn from(err: serde_json::Error) -> Self {
110 PluginError::SerializationError(err.to_string())
111 }
112}
113
114#[cfg(test)]
115#[path = "error/error_tests.rs"]
116mod error_tests;
117
118#[cfg(test)]
119#[path = "error/error_parameterized_tests.rs"]
120mod error_parameterized_tests;