Skip to main content

orion_accessor/addr/
error.rs

1use derive_more::From;
2use orion_error::OrionError;
3use orion_error::{StructError, UnifiedReason};
4use serde_derive::Serialize;
5use std::time::Duration;
6
7#[derive(Clone, Debug, Serialize, PartialEq, From, OrionError)]
8pub enum AddrReason {
9    #[orion_error(identity = "biz.addr.brief", code = 500)]
10    Brief(String),
11    #[orion_error(transparent)]
12    Unified(UnifiedReason),
13    #[orion_error(
14        identity = "biz.addr.operation_timeout",
15        code = 408,
16        message = "operation timed out"
17    )]
18    OperationTimeoutExceeded { timeout: Duration, attempts: u32 },
19    #[orion_error(
20        identity = "biz.addr.total_timeout",
21        code = 408,
22        message = "total timeout exceeded"
23    )]
24    TotalTimeoutExceeded {
25        total_timeout: Duration,
26        elapsed: Duration,
27    },
28    #[orion_error(
29        identity = "biz.addr.retry_exhausted",
30        code = 504,
31        message = "retry exhausted"
32    )]
33    RetryExhausted { attempts: u32, last_error: String },
34}
35
36pub type AddrResult<T> = Result<T, StructError<AddrReason>>;
37pub type AddrError = StructError<AddrReason>;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use orion_error::reason::ErrorCode;
43    use std::time::Duration;
44
45    #[test]
46    fn test_addr_reason_brief() {
47        let reason = AddrReason::Brief("test error".to_string());
48        assert_eq!(reason.error_code(), 500);
49        assert_eq!(reason.to_string(), "brief");
50    }
51
52    #[test]
53    fn test_addr_reason_operation_timeout_exceeded() {
54        let timeout = Duration::from_secs(30);
55        let reason = AddrReason::OperationTimeoutExceeded {
56            timeout,
57            attempts: 3,
58        };
59        assert_eq!(reason.error_code(), 408);
60        assert_eq!(reason.to_string(), "operation timed out");
61    }
62
63    #[test]
64    fn test_addr_reason_total_timeout_exceeded() {
65        let total_timeout = Duration::from_secs(60);
66        let elapsed = Duration::from_secs(65);
67        let reason = AddrReason::TotalTimeoutExceeded {
68            total_timeout,
69            elapsed,
70        };
71        assert_eq!(reason.error_code(), 408);
72        assert_eq!(reason.to_string(), "total timeout exceeded");
73    }
74
75    #[test]
76    fn test_addr_reason_retry_exhausted() {
77        let reason = AddrReason::RetryExhausted {
78            attempts: 5,
79            last_error: "connection failed".to_string(),
80        };
81        assert_eq!(reason.error_code(), 504);
82        assert_eq!(reason.to_string(), "retry exhausted");
83    }
84}