Skip to main content

mago_analyzer/external/
error.rs

1use mago_extension::PayloadError;
2use mago_extension::WorkerError;
3
4#[derive(Debug)]
5pub enum ExternalAnalyzerError {
6    InitializationUnavailable,
7    InitializationPanicked,
8    Worker(WorkerError),
9    Protocol(String),
10    InconsistentRegistration,
11    InconsistentInitialization,
12    DuplicateExtension(String),
13    DuplicatePluginSelector { selector: String, first: String, second: String },
14}
15
16impl ExternalAnalyzerError {
17    #[must_use]
18    pub fn protocol(message: impl Into<String>) -> Self {
19        Self::Protocol(message.into())
20    }
21}
22
23impl std::fmt::Display for ExternalAnalyzerError {
24    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Self::InitializationUnavailable => {
27                formatter.write_str("external analyzer initialization thread is unavailable")
28            }
29            Self::InitializationPanicked => formatter.write_str("external analyzer initialization thread panicked"),
30            Self::Worker(error) => write!(formatter, "external analyzer worker failed: {error}"),
31            Self::Protocol(message) => write!(formatter, "external analyzer protocol error: {message}"),
32            Self::InconsistentRegistration => {
33                formatter.write_str("workers in an extension pool advertised different analyzer registrations")
34            }
35            Self::InconsistentInitialization => {
36                formatter.write_str("workers in an extension pool produced different analyzer initialization stubs")
37            }
38            Self::DuplicateExtension(identifier) => {
39                write!(formatter, "external extension `{identifier}` is registered by more than one host")
40            }
41            Self::DuplicatePluginSelector { selector, first, second } => {
42                write!(formatter, "analyzer plugin selector `{selector}` is shared by plugins `{first}` and `{second}`")
43            }
44        }
45    }
46}
47
48impl std::error::Error for ExternalAnalyzerError {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            Self::Worker(error) => Some(error),
52            _ => None,
53        }
54    }
55}
56
57impl From<WorkerError> for ExternalAnalyzerError {
58    fn from(error: WorkerError) -> Self {
59        Self::Worker(error)
60    }
61}
62
63impl From<PayloadError> for ExternalAnalyzerError {
64    fn from(error: PayloadError) -> Self {
65        Self::Protocol(error.to_string())
66    }
67}
68
69pub(super) fn protocol(message: impl Into<String>) -> ExternalAnalyzerError {
70    ExternalAnalyzerError::Protocol(message.into())
71}