orion-error 0.8.0

Struct Error for Large Project
Documentation
/// Legacy numeric error code.
///
/// New protocol-facing code should use the stable identity generated by
/// `OrionError` (`identity = "biz.xxx"`, `category = ...`) as the machine key.
/// This trait remains for compatibility with older numeric-code integrations
/// and built-in `UnifiedReason` helpers.
pub trait ErrorCode {
    fn error_code(&self) -> i32 {
        500
    }
}

/// Categorisation of an error for protocol-level routing and policy decisions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "lowercase")
)]
pub enum ErrorCategory {
    /// Configuration / environment issue (e.g. missing file, bad config).
    Conf,
    /// Business-logic violation (e.g. validation failure, policy reject).
    Biz,
    /// Internal logic error (e.g. unreachable branch, invariant violation).
    Logic,
    /// System / infrastructure error (e.g. network, disk I/O, upstream timeout).
    Sys,
}

impl ErrorCategory {
    /// Return the stable string code for this error variant.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Conf => "conf",
            Self::Biz => "biz",
            Self::Logic => "logic",
            Self::Sys => "sys",
        }
    }
}

/// Runtime identity provider for stable error codes and categories.
///
/// Implemented automatically by `#[derive(OrionError)]`. Used by
/// [`StructError::exposure`](crate::StructError::exposure)
/// and the protocol projection layer to determine visibility and
/// exposure decisions.
pub trait ErrorIdentityProvider {
    fn stable_code(&self) -> &'static str;

    fn error_category(&self) -> ErrorCategory;
}