orion-error 0.7.1

Struct Error for Large Project
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use thiserror::Error;

use super::{DomainReason, ErrorCategory, ErrorCode, ErrorIdentityProvider};

/// Configuration error sub-classification
/// 配置错误子分类
#[derive(Debug, Error, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum ConfErrReason {
    #[error("core config")]
    Core,
    #[error("feature config error")]
    Feature,
    #[error("dynamic config error")]
    Dynamic,
}

/// Universal error reason classification with clear hierarchical structure
/// 统一错误原因分类 - 采用清晰的分层结构
///
/// # Error Code Ranges
/// - 100-199: Business Layer Errors (业务层错误)
/// - 200-299: Infrastructure Layer Errors (基础设施层错误)
/// - 300-399: Configuration & External Layer Errors (配置和外部层错误)
#[derive(Debug, Error, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum UvsReason {
    // === Business Layer Errors (100-199) ===
    /// Input validation errors (格式错误、参数校验失败等)
    #[error("validation error")]
    ValidationError,

    /// Business logic rule violations (业务规则违反、状态冲突等)
    #[error("business logic error")]
    BusinessError,

    /// Business logic rule violations (业务规则违反、状态冲突等)
    #[error("run rule error")]
    RunRuleError,

    /// Resource not found (查询的资源不存在)
    #[error("not found error")]
    NotFoundError,

    /// Permission and authorization errors (权限不足、认证失败)
    #[error("permission error")]
    PermissionError,

    // === Infrastructure Layer Errors (200-299) ===
    /// Database and data processing errors (数据库操作、数据格式错误)
    #[error("data error")]
    DataError,

    /// File system and OS-level errors (文件系统、操作系统错误)
    #[error("system error")]
    SystemError,

    /// Network connectivity and protocol errors (网络连接、HTTP请求错误)
    #[error("network error")]
    NetworkError,

    /// Resource exhaustion (内存不足、磁盘空间不足等)
    #[error("resource error")]
    ResourceError,

    /// Operation timeouts (操作超时)
    #[error("timeout error")]
    TimeoutError,

    // === Configuration & External Layer Errors (300-399) ===
    /// Configuration-related errors (配置相关错误)
    #[error("configuration error << {0}")]
    ConfigError(ConfErrReason),

    /// Third-party service errors (第三方服务错误)
    #[error("external service error")]
    ExternalError,

    /// Third-party service errors (第三方服务错误)
    #[error("BUG :logic error")]
    LogicError,
}

impl DomainReason for UvsReason {}

impl UvsReason {
    // === Configuration Error Constructors ===
    pub fn core_conf() -> Self {
        Self::ConfigError(ConfErrReason::Core)
    }

    pub fn feature_conf() -> Self {
        Self::ConfigError(ConfErrReason::Feature)
    }

    pub fn dynamic_conf() -> Self {
        Self::ConfigError(ConfErrReason::Dynamic)
    }

    // === Business Layer Constructors ===
    pub fn validation_error() -> Self {
        Self::ValidationError
    }

    pub fn business_error() -> Self {
        Self::BusinessError
    }

    pub fn rule_error() -> Self {
        Self::RunRuleError
    }

    pub fn not_found_error() -> Self {
        Self::NotFoundError
    }

    pub fn permission_error() -> Self {
        Self::PermissionError
    }

    // === Infrastructure Layer Constructors ===
    pub fn data_error() -> Self {
        Self::DataError
    }

    pub fn system_error() -> Self {
        Self::SystemError
    }

    pub fn network_error() -> Self {
        Self::NetworkError
    }

    pub fn resource_error() -> Self {
        Self::ResourceError
    }

    pub fn timeout_error() -> Self {
        Self::TimeoutError
    }

    // === External Layer Constructors ===
    pub fn external_error() -> Self {
        Self::ExternalError
    }

    pub fn logic_error() -> Self {
        Self::LogicError
    }
}

/// Unified constructor helpers for types that can be converted from `UvsReason`.
pub trait UvsFrom: From<UvsReason> + Sized {
    fn from_conf() -> Self {
        Self::from(UvsReason::core_conf())
    }

    fn from_conf_reason(reason: ConfErrReason) -> Self {
        Self::from(UvsReason::ConfigError(reason))
    }

    fn from_data() -> Self {
        Self::from(UvsReason::data_error())
    }

    fn from_sys() -> Self {
        Self::from(UvsReason::system_error())
    }

    fn from_biz() -> Self {
        Self::from(UvsReason::business_error())
    }

    fn from_logic() -> Self {
        Self::from(UvsReason::logic_error())
    }

    fn from_rule() -> Self {
        Self::from(UvsReason::rule_error())
    }

    fn from_res() -> Self {
        Self::from(UvsReason::resource_error())
    }

    fn from_net() -> Self {
        Self::from(UvsReason::network_error())
    }

    fn from_timeout() -> Self {
        Self::from(UvsReason::timeout_error())
    }

    fn from_validation() -> Self {
        Self::from(UvsReason::validation_error())
    }

    fn from_not_found() -> Self {
        Self::from(UvsReason::not_found_error())
    }

    fn from_permission() -> Self {
        Self::from(UvsReason::permission_error())
    }

    fn from_external() -> Self {
        Self::from(UvsReason::external_error())
    }
}

impl<T> UvsFrom for T where T: From<UvsReason> {}

impl ErrorCode for UvsReason {
    fn error_code(&self) -> i32 {
        match self {
            // === Business Layer Errors (100-199) ===
            UvsReason::ValidationError => 100,
            UvsReason::BusinessError => 101,
            UvsReason::NotFoundError => 102,
            UvsReason::PermissionError => 103,
            UvsReason::LogicError => 104,
            UvsReason::RunRuleError => 105,

            // === Infrastructure Layer Errors (200-299) ===
            UvsReason::DataError => 200,
            UvsReason::SystemError => 201,
            UvsReason::NetworkError => 202,
            UvsReason::ResourceError => 203,
            UvsReason::TimeoutError => 204,

            // === Configuration & External Layer Errors (300-399) ===
            UvsReason::ConfigError(_) => 300,
            UvsReason::ExternalError => 301,
        }
    }
}

impl ErrorIdentityProvider for UvsReason {
    fn stable_code(&self) -> &'static str {
        match self {
            UvsReason::ValidationError => "biz.validation_error",
            UvsReason::BusinessError => "biz.business_error",
            UvsReason::RunRuleError => "biz.run_rule_error",
            UvsReason::NotFoundError => "biz.not_found",
            UvsReason::PermissionError => "biz.permission_denied",
            UvsReason::DataError => "sys.data_error",
            UvsReason::SystemError => "sys.io_error",
            UvsReason::NetworkError => "sys.network_error",
            UvsReason::ResourceError => "sys.resource_exhausted",
            UvsReason::TimeoutError => "sys.timeout",
            UvsReason::ConfigError(ConfErrReason::Core) => "conf.core_invalid",
            UvsReason::ConfigError(ConfErrReason::Feature) => "conf.feature_invalid",
            UvsReason::ConfigError(ConfErrReason::Dynamic) => "conf.dynamic_invalid",
            UvsReason::ExternalError => "sys.external_service_error",
            UvsReason::LogicError => "logic.internal_invariant_broken",
        }
    }

    fn error_category(&self) -> ErrorCategory {
        match self {
            UvsReason::ConfigError(_) => ErrorCategory::Conf,
            UvsReason::LogicError => ErrorCategory::Logic,
            UvsReason::ValidationError
            | UvsReason::BusinessError
            | UvsReason::RunRuleError
            | UvsReason::NotFoundError
            | UvsReason::PermissionError => ErrorCategory::Biz,
            UvsReason::DataError
            | UvsReason::SystemError
            | UvsReason::NetworkError
            | UvsReason::ResourceError
            | UvsReason::TimeoutError
            | UvsReason::ExternalError => ErrorCategory::Sys,
        }
    }
}

impl UvsReason {
    /// Check if this error is retryable
    /// 检查错误是否可重试
    pub fn is_retryable(&self) -> bool {
        match self {
            // Infrastructure errors are often retryable
            UvsReason::NetworkError => true,
            UvsReason::TimeoutError => true,
            UvsReason::ResourceError => true,
            UvsReason::SystemError => true,
            UvsReason::ExternalError => true,

            // Business logic errors are generally not retryable
            UvsReason::ValidationError => false,
            UvsReason::BusinessError => false,
            UvsReason::RunRuleError => false,
            UvsReason::NotFoundError => false,
            UvsReason::PermissionError => false,

            // Configuration errors require manual intervention
            UvsReason::ConfigError(_) => false,
            UvsReason::DataError => false,
            UvsReason::LogicError => false,
        }
    }

    /// Check if this error should be logged with high severity
    /// 检查错误是否需要高优先级记录
    pub fn is_high_severity(&self) -> bool {
        match self {
            // System and infrastructure issues are high severity
            UvsReason::SystemError => true,
            UvsReason::ResourceError => true,
            UvsReason::ConfigError(_) => true,

            // Others are normal business operations
            _ => false,
        }
    }

    /// Get error category name for monitoring and metrics
    /// 获取错误类别名称用于监控和指标
    pub fn category_name(&self) -> &'static str {
        match self {
            UvsReason::ValidationError => "validation",
            UvsReason::BusinessError => "business",
            UvsReason::RunRuleError => "runrule",
            UvsReason::NotFoundError => "not_found",
            UvsReason::PermissionError => "permission",
            UvsReason::DataError => "data",
            UvsReason::SystemError => "system",
            UvsReason::NetworkError => "network",
            UvsReason::ResourceError => "resource",
            UvsReason::TimeoutError => "timeout",
            UvsReason::ConfigError(_) => "config",
            UvsReason::ExternalError => "external",
            UvsReason::LogicError => "logic",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_code_ranges() {
        // Business layer (100-199)
        assert_eq!(UvsReason::validation_error().error_code(), 100);
        assert_eq!(UvsReason::business_error().error_code(), 101);
        assert_eq!(UvsReason::not_found_error().error_code(), 102);
        assert_eq!(UvsReason::permission_error().error_code(), 103);

        // Infrastructure layer (200-299)
        assert_eq!(UvsReason::data_error().error_code(), 200);
        assert_eq!(UvsReason::system_error().error_code(), 201);
        assert_eq!(UvsReason::network_error().error_code(), 202);
        assert_eq!(UvsReason::resource_error().error_code(), 203);
        assert_eq!(UvsReason::timeout_error().error_code(), 204);

        // Configuration & external layer (300-399)
        assert_eq!(UvsReason::core_conf().error_code(), 300);
        assert_eq!(UvsReason::external_error().error_code(), 301);
    }

    #[test]
    fn test_retryable_errors() {
        assert!(UvsReason::network_error().is_retryable());
        assert!(UvsReason::timeout_error().is_retryable());
        assert!(!UvsReason::validation_error().is_retryable());
        assert!(!UvsReason::business_error().is_retryable());
    }

    #[test]
    fn test_high_severity_errors() {
        assert!(UvsReason::system_error().is_high_severity());
        assert!(UvsReason::resource_error().is_high_severity());
        assert!(!UvsReason::validation_error().is_high_severity());
        assert!(!UvsReason::NotFoundError.is_high_severity());
    }

    #[test]
    fn test_category_names() {
        assert_eq!(UvsReason::network_error().category_name(), "network");
        assert_eq!(UvsReason::business_error().category_name(), "business");
        assert_eq!(UvsReason::core_conf().category_name(), "config");
    }

    #[test]
    fn test_stable_code_values() {
        assert_eq!(
            UvsReason::validation_error().stable_code(),
            "biz.validation_error"
        );
        assert_eq!(UvsReason::system_error().stable_code(), "sys.io_error");
        assert_eq!(UvsReason::core_conf().stable_code(), "conf.core_invalid");
        assert_eq!(
            UvsReason::logic_error().stable_code(),
            "logic.internal_invariant_broken"
        );
    }

    #[test]
    fn test_error_categories() {
        assert_eq!(
            UvsReason::validation_error().error_category(),
            ErrorCategory::Biz
        );
        assert_eq!(
            UvsReason::system_error().error_category(),
            ErrorCategory::Sys
        );
        assert_eq!(UvsReason::core_conf().error_category(), ErrorCategory::Conf);
        assert_eq!(
            UvsReason::logic_error().error_category(),
            ErrorCategory::Logic
        );
        assert_eq!(ErrorCategory::Biz.as_str(), "biz");
    }

    #[test]
    fn test_trait_implementations() {
        let reason: UvsReason = <UvsReason as UvsFrom>::from_net();
        assert_eq!(reason.error_code(), 202);

        let reason: UvsReason = <UvsReason as UvsFrom>::from_validation();
        assert_eq!(reason.error_code(), 100);

        let reason: UvsReason = <UvsReason as UvsFrom>::from_external();
        assert_eq!(reason.error_code(), 301);
        assert_eq!(reason.error_category(), ErrorCategory::Sys);
    }
}