Orion error engineering

From galaxio-labs/orion-skills · View interactive version

Orion error engineering is an AI agent skill from the GitHub repository galaxio-labs/orion-skills. Use when working with the Rust crate orion-error: adopting it in services, defining OrionError enums, choosing UnifiedReason categories, adding StructError context, using ToStructError/SourceErr/ConvErr correctly, leveraging transparent UnifiedReason delegate constructors, integrating report/protoco

How to use this skill

Skill file

Orion Error Engineering

Use this skill for source-accurate work with the orion-error Rust crate.

First Checks

  1. Locate the crate root. In this workspace it is usually orion-error/.
  2. Treat src/, tests/, and compiling examples as source of truth.
  3. For 0.9.x, prefer orion_error::prelude::* plus small layered imports.
  4. When behavior changes, verify with cargo test --all-features -- --test-threads=1 from the crate root.

Current API Facts

Recommended 0.9 Workflow

  1. Define a domain enum with #[derive(OrionError)].
  2. Give business variants stable #[orion_error(identity = "...")] identities.
  3. Keep a transparent General(UnifiedReason) variant for shared infrastructure categories.

#[derive(OrionError)] exposes the full set of UnifiedReason delegate constructors on the outer enum, so prefer calls like AppReason::system_error() or AppReason::validation_error() over AppReason::from(UnifiedReason::system_error()).

  1. Use OperationContext::doing(...) plus chained with_field(...) / with_meta(...) for structured context.
  2. For a single domain reason turning into StructError<R>, prefer to_err().
  3. For a normal Result<T, E> entering the structured system the first time, prefer source_err(reason, detail) when E: UnstructuredSource (e.g. std::io::Error).
  4. For third-party StdError types that don't implement UnstructuredSource (e.g. git2::Error, reqwest::Error), use source_raw_err(reason, detail).
  5. For an upstream Result<T, StructError<R1>> that only changes reason type, use conv_err().
  6. For an upstream error where the upper layer wants a new semantic boundary, use source_err(reason, detail).
  7. For real underlying errors, prefer the Result-level entry points source_err(...) / source_raw_err(...); when building a StructError directly, use StructErrorBuilder::source_std(...) / source_struct(...).
  8. For stable machine identity, prefer identity_snapshot() / ErrorIdentity.code.
  9. For protocol projection, prefer exposure(...) / into_exposure(...) and to_*_error_json() when serde_json is enabled.
  10. For human diagnostics and redaction, prefer report(), render(), render_redacted(...), and render_user_debug() on protocol snapshots.

Decision Tree

Existing-System Migration Branch

Use this branch when upgrading an existing crate or service with old error APIs, anyhow::Result, broad map_err, or payload-carrying reason enums.

  1. Upgrade the orion-error dependency and imports first.
  2. Replace obvious legacy APIs such as .owe(), owe_*(), into_as(...), upcast(), wrap_as(...), err_wrap(...), want(...), and with(...).
  3. Convert boundary errors next:
  1. Replace pure wrapping map_err calls with source_err(...), source_raw_err(...), or conv_err().
  2. Keep payload-carrying reason enums temporarily during the API migration so compile errors stay focused on API changes.
  3. After tests stabilize, migrate domain reasons such as RunReason, ExecReason, and GxlReason toward unit enum variants.
  4. Move the old payload information into StructError detail, context, fields, metadata, or source chains.

Unit enum reason modeling is the semantic-convergence phase, not the first API-upgrade step. Doing it too early mixes constructor breakage with diagnostic relocation and makes migrations harder to review.

MapErr Migration Rules

Domain Enum Guidance

Import Guidance

Exposure, Report, Protocol

Operation Logging (0.9)

As of 0.9, operation logging is split into a data role and a guard role. This is the sanctioned way to emit lifecycle (suc! / fail! / cancel!) logs.

Reasonable approach:

  1. Build the operation as pure data, then arm a guard only where you want lifecycle logging:

```rust

let ctx = OperationContext::doing("renew").with_field("gateway_id", id);

let guard = ctx.with_auto_log();

```

  1. For fallible flows, use scope() (defaults to failure) plus mark_success(). An early ? return skips mark_success(), so the failure is logged correctly:

```rust

let mut guard = OperationContext::doing("renew").with_auto_log();

let mut scope = guard.scope(); // default: failure

let value = fetch().await?; // early return -> fail!

scope.mark_success(); // success -> suc!

```

Avoid scoped_success() in ?-heavy code: it defaults to success, so an early return is logged as suc!.

  1. To attach the operation to an error as data, borrow the guard: err.with_context(&guard). This copies data and emits no log; the guard stays the single log owner. There is intentionally no by-value with_context(guard) (that would defer or repeat the log). Use guard.into_context() to disarm and keep only the data.
  1. Do not add error! / info! at every layer; the model is sparse lifecycle logs plus boundary error projection.

Migration from 0.8:

Features:

Docs And Site Guidance

Stale-Doc Guardrails

References

Read references/patterns.md when you need copyable Rust templates, and references/logging.md for the sanctioned 0.9 operation-logging patterns.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.