mago-analyzer 1.47.1

A PHP static analyzer that can detect type errors in PHP code, and provide suggestions for fixing them.
Documentation
//! Error types for the plugin system.

use std::fmt;
use std::sync::Arc;

use crate::external::ExternalAnalyzerError;

/// Result type for plugin operations.
pub type PluginResult<T> = Result<T, PluginError>;

/// Errors that can occur during plugin operations.
#[derive(Debug, Clone)]
pub enum PluginError {
    /// An external analyzer worker, transport, or protocol operation failed.
    External(Arc<ExternalAnalyzerError>),

    /// Plugin initialization failed.
    InitializationFailed { name: String, reason: String },

    /// Plugin returned an invalid result.
    InvalidResult { plugin: String, operation: String, reason: String },

    /// Plugin configuration error.
    Configuration { plugin: String, reason: String },

    /// Internal plugin error.
    Internal { reason: String },
}

impl fmt::Display for PluginError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PluginError::External(error) => write!(f, "External plugin error: {error}"),
            PluginError::InitializationFailed { name, reason } => {
                write!(f, "Plugin '{name}' failed to initialize: {reason}")
            }
            PluginError::InvalidResult { plugin, operation, reason } => {
                write!(f, "Plugin '{plugin}' returned invalid result for '{operation}': {reason}")
            }
            PluginError::Configuration { plugin, reason } => {
                write!(f, "Plugin '{plugin}' configuration error: {reason}")
            }
            PluginError::Internal { reason } => {
                write!(f, "Internal plugin error: {reason}")
            }
        }
    }
}

impl std::error::Error for PluginError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::External(error) => Some(error.as_ref()),
            _ => None,
        }
    }
}

impl From<Arc<ExternalAnalyzerError>> for PluginError {
    fn from(error: Arc<ExternalAnalyzerError>) -> Self {
        Self::External(error)
    }
}

impl From<ExternalAnalyzerError> for PluginError {
    fn from(error: ExternalAnalyzerError) -> Self {
        Self::External(Arc::new(error))
    }
}