Skip to main content

orion_error/core/
reason.rs

1/// Legacy numeric error code.
2///
3/// New protocol-facing code should use the stable identity generated by
4/// `OrionError` (`identity = "biz.xxx"`, `category = ...`) as the machine key.
5/// This trait remains for compatibility with older numeric-code integrations
6/// and built-in `UnifiedReason` helpers.
7pub trait ErrorCode {
8    fn error_code(&self) -> i32 {
9        500
10    }
11}
12
13/// Categorisation of an error for protocol-level routing and policy decisions.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[cfg_attr(
16    feature = "serde",
17    derive(serde::Serialize, serde::Deserialize),
18    serde(rename_all = "lowercase")
19)]
20pub enum ErrorCategory {
21    /// Configuration / environment issue (e.g. missing file, bad config).
22    Conf,
23    /// Business-logic violation (e.g. validation failure, policy reject).
24    Biz,
25    /// Internal logic error (e.g. unreachable branch, invariant violation).
26    Logic,
27    /// System / infrastructure error (e.g. network, disk I/O, upstream timeout).
28    Sys,
29}
30
31impl ErrorCategory {
32    /// Return the stable string code for this error variant.
33    pub fn as_str(self) -> &'static str {
34        match self {
35            Self::Conf => "conf",
36            Self::Biz => "biz",
37            Self::Logic => "logic",
38            Self::Sys => "sys",
39        }
40    }
41}
42
43/// Runtime identity provider for stable error codes and categories.
44///
45/// Implemented automatically by `#[derive(OrionError)]`. Used by
46/// [`StructError::exposure`](crate::StructError::exposure)
47/// and the protocol projection layer to determine visibility and
48/// exposure decisions.
49pub trait ErrorIdentityProvider {
50    fn stable_code(&self) -> &'static str;
51
52    fn error_category(&self) -> ErrorCategory;
53}