waddling-errors 🦆
Type-safe diagnostic code system with structured error codes
Overview
waddling-errors provides structured diagnostic codes with a consistent 4-part format:
SEVERITY.COMPONENT.PRIMARY.SEQUENCE
E.AUTH.TOKEN.001 // Error in Auth component, Token category, sequence 001
W.PARSER.SYNTAX.003 // Warning in Parser component, Syntax category, sequence 003
S.BUILD.DONE.999 // Success in Build component, Done category, sequence 999
Generated Documentation
Auto-generated role-based error documentation with severity filtering, search, and detailed hints
Key Features
- ✅ Type-safe - Define your own component/primary enums with compile-time checking
- ✅ Zero runtime dependencies - Pure Rust with optional features (proc macros are compile-time only)
- ✅ Semantic methods -
is_blocking(),is_positive(),priority()for intelligent error handling - ✅ Documentation generation - Auto-generate JSON/HTML/Catalog docs with role-based filtering
- ✅ Hash codes - Optional 5-character base62 hashes for compact logging
- ✅ Network efficiency - Catalog renderer for 80% smaller payloads (IoT/mobile)
- ✅
no_stdcompatible - Works in embedded and WASM environments
Installation
[]
# Default (no_std + alloc compatible, includes macros)
= "0.7"
# With documentation generation (requires std)
= { = "0.7", = ["std", "doc-gen", "hash", "serde"] }
# Pure no_std (no allocator)
= { = "0.7", = false }
# For procedural macros separately
= "0.7"
Default features: ["macros"] - Works in no_std + alloc environments.
See docs/FEATURE_FLAGS.md for detailed feature information.
Quick Start
Macro Approach (Recommended)
Minimal boilerplate with automatic documentation generation:
use ;
// Define your error structure
component!
primary!
// Define diagnostics with auto-registration
diag!
// Use in your code
let error = E_AUTH_TOKEN_EXPIRED;
println!; // "E.AUTH.TOKEN.EXPIRED"
See waddling-errors-macros for complete macro documentation.
Manual Approach (Full Control)
For maximum flexibility, use the core trait-based approach:
use *;
// 1. Define your error structure
// 2. Create error codes
const ERR_TOKEN_MISSING: =
error;
// 3. Use them
Severity Levels
Nine severity levels with semantic methods:
| Severity | Code | Priority | Blocking | Use Case |
|---|---|---|---|---|
| Error | E |
8 | Yes | Invalid input, logic errors |
| Blocked | B |
7 | Yes | Deadlock, I/O wait, network down |
| Critical | C |
6 | No | Data corruption, resource exhaustion |
| Warning | W |
5 | No | Deprecated API, edge cases |
| Help | H |
4 | No | Helpful suggestions, tips |
| Success | S |
3 | No | Operation succeeded |
| Completed | K |
2 | No | Task/phase finished |
| Info | I |
1 | No | Events, milestones, status |
| Trace | T |
0 | No | Execution traces, probes, timing |
let severity = Error;
severity.is_blocking; // true - stops execution
severity.is_negative; // true - indicates failure
severity.is_positive; // false
severity.priority; // 8 (0-8 scale)
Optional emoji/ANSI color support available with features. See FEATURE_FLAGS.md.
Documentation Generation
Generate searchable HTML and JSON documentation with automatic registration:
Step 1: Define Errors with Auto-Registration
// src/errors/mod.rs
use diag;
diag!
Step 2: Create Doc Generation Binary
// src/bin/doc_gen.rs
use *; // Loads errors, triggers auto-registration
use ;
use registry;
Step 3: Configure in Cargo.toml
[[]]
= "doc-gen"
= "src/bin/doc_gen.rs"
= ["doc-gen", "auto-register"]
Step 4: Generate Documentation
Generates:
myapp-pub.html,myapp-pub.json- Public documentationmyapp-dev.html,myapp-dev.json- Developer documentationmyapp-int.html,myapp-int.json- Internal documentation
Documentation Roles:
Role::Public- End-user facing errors (sanitized, safe)Role::Developer- For contributors (debugging context)Role::Internal- Full team visibility (sensitive details)
Key Benefits:
- ✅ Zero boilerplate - auto-registration handles everything
- ✅ Separate binary - no doc-gen overhead in production
- ✅ Clean main - no error imports polluting your app
See docs/DOC_GENERATION_GUIDE.md for complete guide.
Network Efficiency (Catalog Renderer)
Generate compact catalogs for efficient error transmission:
use ;
// Generate hash-to-error catalog
let catalog = new;
registry.render?;
// Server sends: {"h":"jGKFp","f":{"temp":"45.2"}} (40 bytes)
// Client expands: "Temperature 45.2°C exceeds threshold"
Benefits:
- 80% smaller network payloads for IoT/mobile
- Multi-language support (same hash, different catalogs)
- Offline-first apps (cache catalog locally)
See docs/CATALOG_GUIDE.md for complete guide.
no_std Support
The library is no_std compatible by default and works in embedded/WASM environments:
Default (no_std + alloc):
[]
= "0.7" # Works with allocator
Pure no_std (no allocator):
[]
= { = "0.7", = false }
Example:
extern crate alloc;
use *;
// Works without std!
const ERR: = error;
For documentation generation (requires std):
= { = "0.7", = ["std", "doc-gen", "metadata"] }
Hash Codes
Enable 5-character base62 hashes for compact logging:
= { = "0.7", = ["hash"] }
const ERR: = error;
println!;
// E.AUTH.TOKEN.001 → #jGKFp
// Use in logs for quick searches
error!;
Sequence Conventions
Semantic sequence numbers provide consistency across projects:
| Sequence | Meaning | Example |
|---|---|---|
| 001 | MISSING | Required item not provided |
| 003 | INVALID | Format/validation failed |
| 007 | DUPLICATE | Rate limit or duplicate entry |
| 008 | DENIED | Permission/access denied |
| 021 | NOTFOUND | Resource not found |
| 025 | CORRUPTED | Data corruption detected |
| 999 | COMPLETE | Full completion |
See docs/SEQUENCE-CONVENTIONS.md for the complete list.
Examples
Basic usage:
With documentation metadata:
Production pattern (6 components, 31 errors, role-based docs):
HTML customization:
no_std/WASM:
# Pure no_std
# With no_std example
See examples/ directory for all examples.
Documentation
User Guides
- Feature Flags - Optional features and configuration
- Sequence Conventions - Semantic sequence patterns
- Catalog Guide - Network efficiency & i18n
- Doc Generation Guide - Documentation workflow
- HTML Customization - Branding your docs
API Reference
- API Documentation - Complete API reference on docs.rs
Related Crates
- waddling-errors-macros - Procedural macros (recommended)
- waddling-errors-hash - Hash generation implementation
License
Dual-licensed under MIT or Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.