Skip to main content

rustbridge_core/
error.rs

1//! Error types for rustbridge plugins
2
3use thiserror::Error;
4
5/// Result type alias for plugin operations
6pub type PluginResult<T> = Result<T, PluginError>;
7
8/// Error type for plugin operations
9#[derive(Error, Debug)]
10pub enum PluginError {
11    /// Plugin is not in a valid state for the requested operation
12    #[error("invalid lifecycle state: expected {expected}, got {actual}")]
13    InvalidState { expected: String, actual: String },
14
15    /// Failed to initialize the plugin
16    #[error("initialization failed: {0}")]
17    InitializationFailed(String),
18
19    /// Failed to shutdown the plugin
20    #[error("shutdown failed: {0}")]
21    ShutdownFailed(String),
22
23    /// Configuration error
24    #[error("configuration error: {0}")]
25    ConfigError(String),
26
27    /// Serialization/deserialization error
28    #[error("serialization error: {0}")]
29    SerializationError(String),
30
31    /// Unknown message type tag
32    #[error("unknown message type: {0}")]
33    UnknownMessageType(String),
34
35    /// Handler returned an error
36    #[error("handler error: {0}")]
37    HandlerError(String),
38
39    /// Async runtime error
40    #[error("runtime error: {0}")]
41    RuntimeError(String),
42
43    /// Request was cancelled
44    #[error("request cancelled")]
45    Cancelled,
46
47    /// Request timed out
48    #[error("request timed out")]
49    Timeout,
50
51    /// Internal error
52    #[error("internal error: {0}")]
53    Internal(String),
54
55    /// FFI error
56    #[error("FFI error: {0}")]
57    FfiError(String),
58
59    /// Too many concurrent requests
60    #[error("too many concurrent requests (limit exceeded)")]
61    TooManyRequests,
62}
63
64impl PluginError {
65    /// Returns an error code suitable for FFI
66    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    /// Create an error from an error code and message (for FFI deserialization)
85    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;