orion-error
Structured error governance for large Rust codebases.
orion-error is not just an error type library.
It is a governance framework for large Rust services and multi-layer systems. It helps teams move from ad-hoc strings and mixed local conventions to one shared error model for:
- semantic modeling
- runtime propagation
- context attachment
- cross-layer conversion
- boundary-facing output for HTTP / RPC / CLI / logs
Core building blocks:
- stable business identities via
#[derive(OrionError)] - one runtime carrier:
StructError<R> - explicit first-entry conversion with
source_err(...) - unified error entry point: .source_err(...)` for all source types
- report and exposure helpers for service boundaries
Why It Is Useful
Use this crate when you want:
- one shared error language across service / repo / adapter / protocol layers
- clear business error enums instead of scattered strings
- one consistent way to attach detail, source, and operation context
- stable machine-facing identity for HTTP / RPC / log / CLI boundaries
- controlled bridging to
std::error::Erroronly where needed - a system that scales better than local
Result<T, String>habits
If you only need a tiny local enum inside one module, thiserror alone may be
enough. If your service has layers, external boundaries, and structured error
output, orion-error is the better fit.
In short:
thiserroris a good local modeling toolorion-erroris for project-wide error governance
Install
[]
= "0.8"
Default features include derive and log — add a feature only when you need it.
Quick Start
use From;
use ;
What happens here:
AppReasonis your domain reason enumStructError<AppReason>is the runtime error carrier- .source_err(...)` converts a normal Rust error into the structured system
doing(...)andwith_context(...)add operation context
For new code, treat doing(...) as the standard operation verb.
The 4 APIs To Learn First
#[derive(OrionError)]Define stable business-facing reason enums.- .source_err(reason, detail)
Use when an error enters the structured system — works for both rawstd::error::Errorand already-structuredStructError<_>` sources. conv_err()Use when the upstream value is alreadyStructError<R1>and you only remap reason type toStructError<R2>.exposure(&policy)Use at service boundaries to project the error into HTTP/RPC/CLI/log output.
Typical Flow
raw std error ──→.source_err(...) ──→ first entry into structured system
│
conv_err()
(reason remap)
│
report / exposure
This is the important shift:
- lower layers do not invent random output shapes
- middle layers do not lose source and context
- boundary layers do not re-interpret raw strings
- the whole system shares one governance model
Service Boundary Helpers
When you reach HTTP/RPC/log/CLI boundaries, these are the main entry points:
report()for human-oriented diagnosticsidentity_snapshot()for stable identity inspectionexposure(...)withto_http_error_json(),to_cli_error_json(),to_log_error_json(),to_rpc_error_json()
Current protocol naming is Exposure*, not ErrorPolicy*.
That matters because large systems usually fail at the boundary:
- one team exposes too much detail
- another team hides everything
- every protocol builds its own error schema
orion-error gives those boundaries one consistent projection model.
Third-Party Error Types
source_err supports built-in types (io::Error, serde_json::Error, anyhow::Error,
toml::Error) and custom types via opt-in:
use ;
use *;
;
// Step 1: declare it as a raw source
// Step 2: wrap + convert
let result: = Err;
let err = result
.map_err
.source_err
.unwrap_err;
assert_eq!;
Why opt-in instead of blanket
E: StdError? A blanket impl would silently swallowStructError<_>values as unstructured sources, losing their structured identity and context. The opt-in ensures you explicitly choose which types enter as unstructured sources versus structured ones.
Newtype wrapper for foreign types. If the error type comes from a dependency
and you cannot implement RawStdError directly (orphan rule), use a newtype:
use ;
use *;
;
;
// Usage
let result: = Err;
let err = result
.map_err
.source_err
.unwrap_err;
assert_eq!;
Standard Error Interop
StructError<R> no longer directly implements std::error::Error.
Use the explicit interop APIs when you need that ecosystem:
use ;
let borrowed_err = from;
let owned_err = from;
let boxed_err = from;
let borrowed_std = borrowed_err.as_std;
let owned_std = owned_err.into_std;
let boxed_std = boxed_err.into_boxed_std;
assert!;
assert!;
assert!;
Recommended Imports
For new code, start with:
use *;
Treat this as the default for business code. Only switch to layered imports when the module is explicitly modeling architecture boundaries, protocol adapters, or test/schema checks.
Then add only the layered imports you need, for example:
orion_error::runtime::OperationContextorion_error::runtime::source::*orion_error::report::*orion_error::protocol::*
This keeps normal application code on one predictable entry path while still letting larger codebases keep clear module boundaries where that extra precision is useful.
Import Strategy
Three tiers:
Application code (default)
use *;
use OperationContext;
Architecture boundaries — use layered imports to make module coupling explicit.
// Domain layer
use *;
use ;
// Service / adapter layer — struct error is your carrier
use ;
// Protocol / boundary layer — output projection only
use *;
use ;
use *;
// Interop — when you must enter std::error::Error ecosystem
use *;
Test / migration
use *;
use *;
Error Flow Paths
There are exactly four ways a StructError enters or moves through your system:
raw std error / StructError ──→.source_err(reason, detail) ──→ first entry
│
conv_err()
(reason remap)
│
report / exposure
**1. .source_err(reason, detail)** — unified entry point. Works for both raw std::error::Errorand already-structuredStructError` sources. Use this
whenever an error enters your system.
2. conv_err() — cross-layer conversion preserving semantics. The upstream error is
already StructError<R1>; you only want to map the reason type to StructError<R2> via
From. All detail, context, source, and metadata survive.
3. as_std() / into_std() / into_dyn_std() — exit point. Bridges the structured error
into the std::error::Error ecosystem for interop or legacy interfaces. These are
explicit; StructError<T> does not implement StdError directly.
Optional Features
Add features only when your project needs them:
[]
= { = "0.8", = ["serde"] } # Serialize/Deserialize
= { = "0.8", = ["serde_json"] } # Protocol JSON projections
= { = "0.8", = ["tracing"] } # Tracing integration
= { = "0.8", = ["anyhow"] } # anyhow::Error interop
= { = "0.8", = ["toml"] } # toml::Error interop
serde, serde_json, tracing, anyhow, toml are optional. The default (derive + log) covers the core path.
Try It
Learn More
- 中文 README
- Changelog
- English docs
- 中文文档
- Tutorial
- Protocol Contract
- thiserror Comparison
- orion-error-derive README
Maintainers
If publishing this crate family:
- publish
orion-error-derive - wait for crates.io index propagation
- publish
orion-error