Skip to main content

camel_component_validator/
error.rs

1use std::sync::Arc;
2
3use thiserror::Error;
4
5use crate::proto;
6
7#[derive(Error, Debug, Clone)]
8pub enum ValidatorError {
9    #[error("{0}")]
10    Endpoint(String),
11    #[error("{0}")]
12    Validation(String),
13    #[error("{message}")]
14    Transport {
15        message: String,
16        #[source]
17        source: Option<Arc<dyn std::error::Error + Send + Sync>>,
18    },
19    #[error("{0}")]
20    UnsupportedMode(&'static str),
21    #[error("{message}")]
22    CompilationFailed {
23        message: String,
24        #[source]
25        source: Option<Arc<dyn std::error::Error + Send + Sync>>,
26    },
27    #[error("payload too large: {actual} bytes exceeds limit of {limit} bytes")]
28    PayloadTooLarge { actual: usize, limit: usize },
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn transport_variant_preserves_source_chain() {
37        let inner = std::io::Error::other("socket closed");
38        let err = ValidatorError::transport_with_source("rpc failed", inner);
39        let source = std::error::Error::source(&err);
40        assert!(source.is_some());
41        assert!(source.unwrap().to_string().contains("socket closed"));
42    }
43}
44
45impl ValidatorError {
46    pub fn endpoint(msg: impl Into<String>) -> Self {
47        Self::Endpoint(msg.into())
48    }
49
50    pub fn validation(msg: impl Into<String>) -> Self {
51        Self::Validation(msg.into())
52    }
53
54    pub fn transport_with_source(
55        msg: impl Into<String>,
56        source: impl std::error::Error + Send + Sync + 'static,
57    ) -> Self {
58        Self::Transport {
59            message: msg.into(),
60            source: Some(Arc::new(source)),
61        }
62    }
63
64    pub fn from_bridge_error(err: &proto::BridgeError) -> Self {
65        let msg = err.message.clone();
66        match proto::bridge_error::Kind::try_from(err.kind)
67            .unwrap_or(proto::bridge_error::Kind::Unknown)
68        {
69            proto::bridge_error::Kind::CompilationFailed => Self::CompilationFailed {
70                message: msg,
71                source: None,
72            },
73            proto::bridge_error::Kind::ValidationFailed => Self::Validation(msg),
74            proto::bridge_error::Kind::Unknown
75            | proto::bridge_error::Kind::InvalidInput
76            | proto::bridge_error::Kind::TransformFailed
77            | proto::bridge_error::Kind::ResourceNotFound
78            | proto::bridge_error::Kind::SecurityViolation
79            | proto::bridge_error::Kind::Internal => Self::Transport {
80                message: msg,
81                source: None,
82            },
83        }
84    }
85
86    pub fn to_endpoint_error(&self) -> camel_component_api::CamelError {
87        camel_component_api::CamelError::EndpointCreationFailed(self.to_string())
88    }
89
90    pub fn to_processor_error(&self) -> camel_component_api::CamelError {
91        camel_component_api::CamelError::ProcessorError(self.to_string())
92    }
93}