luhtwin
Horrible Error Handling for Rust
luhtwin provides a horrible, non-ergonomic error handling system that emphasizes context accumulation, structured diagnostics, and flexible formatting. Built around the AnyError type, it allows you to wrap any error with rich metadata and progressively add context as errors bubble up through your application.
Core Concepts
AnyError— The main error container that wraps anyErrortype with context chainsErrorContext— Structured metadata including messages, file/line info, docs, and severityAnyErrorBuilder— Builder for constructing AnyErrorsLuhTwin<T>— Type alias forResult<T, AnyError>, the primary result type
Features
🔗 Context Chaining
Add contextual information at each layer of your application:
use ;
📝 Error Metadata
Attach documentation links, issue trackers, custom metadata, and severity levels:
use ;
let err = anyerror!
.doc_link
.issues
.metadata
.metadata
.severity
.build;
Multiple Display Formats
Choose the right format for your use case:
display_pretty()— Colorful terminal outputdisplay_full()— Complete diagnostic report with backtracedisplay_contexts_tree()— Hierarchical context visualizationto_log_format()— Structured logging format
Quick Start
Installation
Add luhtwin to your Cargo.toml:
[]
= "0.0.4"
Basic Error Creation
use ;
Adding Context to Existing Errors
use ;
Working with Context Chains
use ;
// Error will contain all three contexts when displayed
Macros
at!— Create anErrorContextat the current file/lineanyerror!— Create anAnyErrorBuilderbail!— Return early with an errorensure!— Assert a condition or return an errorcontext!— Add context to a result
Extension Traits
Context— Add context to anyResult<T, E>whereE: ErrorMapErrExt— Map errors with additional contextLogError— Convenient error logging methods
Error Display Examples
Pretty Display (for terminals)
ERROR error: Failed to connect to database
--> src/db.rs:45
context chain:
1. Failed to connect to database
2. Network timeout occurred
caused by: Connection refused (os error 111)
Tree Display (hierarchical contexts)
└─ Failed to connect to database
at src/db.rs:45
doc: https://docs.example.com/db-errors
issue: DB-101
├─ Network timeout occurred
at src/network.rs:102
Log Format (structured logging)
message="Failed to connect to database" severity=Critical location="src/db.rs:45" source="Connection refused"
made with love s.c - 2025 :3