Skip to main content

camel_component_validator/
error.rs

1use std::fmt::{Display, Formatter};
2
3use crate::proto;
4
5#[derive(Debug, Clone)]
6pub enum ValidatorError {
7    Endpoint(String),
8    Validation(String),
9    Transport(String),
10    UnsupportedMode(&'static str),
11    CompilationFailed(String),
12}
13
14impl ValidatorError {
15    pub fn endpoint(msg: impl Into<String>) -> Self {
16        Self::Endpoint(msg.into())
17    }
18
19    pub fn validation(msg: impl Into<String>) -> Self {
20        Self::Validation(msg.into())
21    }
22
23    pub fn from_bridge_error(err: &proto::BridgeError) -> Self {
24        let msg = err.message.clone();
25        match proto::bridge_error::Kind::try_from(err.kind)
26            .unwrap_or(proto::bridge_error::Kind::Unknown)
27        {
28            proto::bridge_error::Kind::CompilationFailed => Self::CompilationFailed(msg),
29            proto::bridge_error::Kind::ValidationFailed => Self::Validation(msg),
30            proto::bridge_error::Kind::Unknown
31            | proto::bridge_error::Kind::InvalidInput
32            | proto::bridge_error::Kind::TransformFailed
33            | proto::bridge_error::Kind::ResourceNotFound
34            | proto::bridge_error::Kind::SecurityViolation
35            | proto::bridge_error::Kind::Internal => Self::Transport(msg),
36        }
37    }
38
39    pub fn to_endpoint_error(&self) -> camel_component_api::CamelError {
40        camel_component_api::CamelError::EndpointCreationFailed(self.to_string())
41    }
42
43    pub fn to_processor_error(&self) -> camel_component_api::CamelError {
44        camel_component_api::CamelError::ProcessorError(self.to_string())
45    }
46}
47
48impl Display for ValidatorError {
49    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50        match self {
51            Self::Endpoint(msg) => write!(f, "{msg}"),
52            Self::Validation(msg) => write!(f, "{msg}"),
53            Self::Transport(msg) => write!(f, "{msg}"),
54            Self::UnsupportedMode(msg) => write!(f, "{msg}"),
55            Self::CompilationFailed(msg) => write!(f, "{msg}"),
56        }
57    }
58}
59
60impl std::error::Error for ValidatorError {}