Skip to main content

camel_api/
error.rs

1use thiserror::Error;
2
3/// Core error type for the Camel framework.
4#[derive(Debug, Clone, Error)]
5#[non_exhaustive]
6pub enum CamelError {
7    #[error("Component not found: {0}")]
8    ComponentNotFound(String),
9
10    #[error("Endpoint creation failed: {0}")]
11    EndpointCreationFailed(String),
12
13    #[error("Processor error: {0}")]
14    ProcessorError(String),
15
16    #[error("Type conversion failed: {0}")]
17    TypeConversionFailed(String),
18
19    #[error("Invalid URI: {0}")]
20    InvalidUri(String),
21
22    #[error("Channel closed")]
23    ChannelClosed,
24
25    #[error("Route error: {0}")]
26    RouteError(String),
27
28    #[error("IO error: {0}")]
29    Io(String),
30
31    #[error("Dead letter channel failed: {0}")]
32    DeadLetterChannelFailed(String),
33
34    #[error("Circuit breaker open: {0}")]
35    CircuitOpen(String),
36
37    #[error("HTTP operation failed: {status_code} {status_text}")]
38    HttpOperationFailed {
39        status_code: u16,
40        status_text: String,
41        response_body: Option<String>,
42    },
43
44    #[error("Exchange stopped by Stop EIP")]
45    Stopped,
46}
47
48impl From<std::io::Error> for CamelError {
49    fn from(err: std::io::Error) -> Self {
50        CamelError::Io(err.to_string())
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_http_operation_failed_display() {
60        let err = CamelError::HttpOperationFailed {
61            status_code: 404,
62            status_text: "Not Found".to_string(),
63            response_body: Some("page not found".to_string()),
64        };
65        let msg = format!("{err}");
66        assert!(msg.contains("404"));
67        assert!(msg.contains("Not Found"));
68    }
69
70    #[test]
71    fn test_http_operation_failed_clone() {
72        let err = CamelError::HttpOperationFailed {
73            status_code: 500,
74            status_text: "Internal Server Error".to_string(),
75            response_body: None,
76        };
77        let cloned = err.clone();
78        assert!(matches!(
79            cloned,
80            CamelError::HttpOperationFailed {
81                status_code: 500,
82                ..
83            }
84        ));
85    }
86
87    #[test]
88    fn test_stopped_variant_exists_and_is_clone() {
89        let err = CamelError::Stopped;
90        let cloned = err.clone();
91        assert!(matches!(cloned, CamelError::Stopped));
92        assert_eq!(format!("{err}"), "Exchange stopped by Stop EIP");
93    }
94}