Skip to main content

orion_error/core/
reason.rs

1/// Legacy numeric error code.
2///
3/// **The authoritative machine identity is
4/// [`ErrorIdentityProvider::stable_code`] + [`ErrorIdentityProvider::error_category`].**
5/// The numeric `error_code()` is retained only for backward compatibility with
6/// older numeric-code integrations (HTTP status mapping, legacy dashboards) and
7/// for the built-in [`crate::UnifiedReason`] helpers.
8///
9/// # Migration guidance
10///
11/// - Prefer `#[derive(OrionError)]` with `identity = "biz.xxx"` and rely on
12///   `stable_code()` for stable, machine-facing identity.
13/// - Do **not** introduce new numeric-code semantics on top of `error_code()`;
14///   the default (`500`) is a compatibility fallback, not a classification.
15/// - If you must expose a numeric value (e.g. for an HTTP status), derive it from
16///   `ErrorCategory` / `ExposurePolicy` rather than from `error_code()`.
17pub trait ErrorCode {
18    fn error_code(&self) -> i32 {
19        500
20    }
21}
22
23/// Categorisation of an error for protocol-level routing and policy decisions.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25#[cfg_attr(
26    feature = "serde",
27    derive(serde::Serialize, serde::Deserialize),
28    serde(rename_all = "lowercase")
29)]
30pub enum ErrorCategory {
31    /// Configuration / environment issue (e.g. missing file, bad config).
32    Conf,
33    /// Business-logic violation (e.g. validation failure, policy reject).
34    Biz,
35    /// Internal logic error (e.g. unreachable branch, invariant violation).
36    Logic,
37    /// System / infrastructure error (e.g. network, disk I/O, upstream timeout).
38    Sys,
39}
40
41impl ErrorCategory {
42    /// Return the stable string code for this error variant.
43    pub fn as_str(self) -> &'static str {
44        match self {
45            Self::Conf => "conf",
46            Self::Biz => "biz",
47            Self::Logic => "logic",
48            Self::Sys => "sys",
49        }
50    }
51}
52
53/// Runtime identity provider for stable error codes and categories.
54///
55/// Implemented automatically by `#[derive(OrionError)]`. Used by
56/// [`StructError::exposure`](crate::StructError::exposure)
57/// and the protocol projection layer to determine visibility and
58/// exposure decisions.
59pub trait ErrorIdentityProvider {
60    fn stable_code(&self) -> &'static str;
61
62    fn error_category(&self) -> ErrorCategory;
63}