Skip to main content

eggress_pproxy_compat/
error.rs

1use crate::diagnostics::DiagnosticCode;
2
3/// Errors from pproxy compatibility translation.
4///
5/// Each variant maps to a stable [`DiagnosticCode`] via [`CompatError::code()`].
6#[derive(Debug, thiserror::Error)]
7pub enum CompatError {
8    /// Maps to [`DiagnosticCode::UnsupportedProtocol`].
9    #[error("unsupported protocol: {0}")]
10    UnsupportedProtocol(String),
11
12    /// Maps to a code determined by the feature name (see [`DiagnosticCode`] docs).
13    #[error("unsupported feature: {feature}")]
14    UnsupportedFeature {
15        feature: &'static str,
16        detail: String,
17    },
18
19    /// Maps to [`DiagnosticCode::InvalidUriSyntax`].
20    #[error("invalid URI: {message}")]
21    InvalidUri { message: String },
22
23    /// Maps to [`DiagnosticCode::InvalidUriSyntax`].
24    #[error("invalid pproxy arguments: {message}")]
25    InvalidArgs { message: String },
26
27    /// Maps to [`DiagnosticCode::InvalidChainComposition`].
28    #[error("config validation failed: {message}")]
29    ConfigValidation { message: String },
30
31    /// Maps to [`DiagnosticCode::MissingTarget`].
32    #[error("missing required argument: {0}")]
33    MissingArgument(String),
34}
35
36impl CompatError {
37    pub fn unsupported(feature: &'static str, detail: impl Into<String>) -> Self {
38        Self::UnsupportedFeature {
39            feature,
40            detail: detail.into(),
41        }
42    }
43
44    /// Return the stable [`DiagnosticCode`] that classifies this error.
45    pub fn code(&self) -> DiagnosticCode {
46        match self {
47            Self::UnsupportedProtocol(_) => DiagnosticCode::UnsupportedProtocol,
48            Self::UnsupportedFeature { feature, .. } => {
49                crate::diagnostics::classify_unsupported_feature_code(feature)
50            }
51            Self::InvalidUri { .. } => DiagnosticCode::InvalidUriSyntax,
52            Self::InvalidArgs { .. } => DiagnosticCode::InvalidUriSyntax,
53            Self::ConfigValidation { .. } => DiagnosticCode::InvalidChainComposition,
54            Self::MissingArgument(_) => DiagnosticCode::MissingTarget,
55        }
56    }
57}