Skip to main content

mago_analyzer/plugin/
error.rs

1//! Error types for the plugin system.
2
3use std::fmt;
4use std::sync::Arc;
5
6use crate::external::ExternalAnalyzerError;
7
8/// Result type for plugin operations.
9pub type PluginResult<T> = Result<T, PluginError>;
10
11/// Errors that can occur during plugin operations.
12#[derive(Debug, Clone)]
13pub enum PluginError {
14    /// An external analyzer worker, transport, or protocol operation failed.
15    External(Arc<ExternalAnalyzerError>),
16
17    /// Plugin initialization failed.
18    InitializationFailed { name: String, reason: String },
19
20    /// Plugin returned an invalid result.
21    InvalidResult { plugin: String, operation: String, reason: String },
22
23    /// Plugin configuration error.
24    Configuration { plugin: String, reason: String },
25
26    /// Internal plugin error.
27    Internal { reason: String },
28}
29
30impl fmt::Display for PluginError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            PluginError::External(error) => write!(f, "External plugin error: {error}"),
34            PluginError::InitializationFailed { name, reason } => {
35                write!(f, "Plugin '{name}' failed to initialize: {reason}")
36            }
37            PluginError::InvalidResult { plugin, operation, reason } => {
38                write!(f, "Plugin '{plugin}' returned invalid result for '{operation}': {reason}")
39            }
40            PluginError::Configuration { plugin, reason } => {
41                write!(f, "Plugin '{plugin}' configuration error: {reason}")
42            }
43            PluginError::Internal { reason } => {
44                write!(f, "Internal plugin error: {reason}")
45            }
46        }
47    }
48}
49
50impl std::error::Error for PluginError {
51    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
52        match self {
53            Self::External(error) => Some(error.as_ref()),
54            _ => None,
55        }
56    }
57}
58
59impl From<Arc<ExternalAnalyzerError>> for PluginError {
60    fn from(error: Arc<ExternalAnalyzerError>) -> Self {
61        Self::External(error)
62    }
63}
64
65impl From<ExternalAnalyzerError> for PluginError {
66    fn from(error: ExternalAnalyzerError) -> Self {
67        Self::External(Arc::new(error))
68    }
69}