Module :: error_tools
A unified error handling facade that provides a consistent interface for both typed and untyped error handling in Rust. error_tools
acts as a standardized wrapper around the popular thiserror
and anyhow
crates, enabling you to write error-handling code once and use it consistently across different contexts.
Why error_tools?
When building Rust applications and libraries, you often face these error handling challenges:
- Library vs Application Choice: Libraries typically use
thiserror
for typed errors, while applications preferanyhow
for flexibility - Inconsistent Error Patterns: Different crates in your dependency tree use different error handling approaches
- Dependency Fragmentation: Having both
anyhow
andthiserror
as direct dependencies across multiple crates - Context Switching: Different syntax and patterns for similar error handling tasks
- Integration Friction: Converting between different error types when bridging library and application code
error_tools solves these problems by providing:
- 🎯 Unified Interface: Single import pattern for both typed and untyped errors
- 📦 Dependency Facade: Centralized re-export of
anyhow
andthiserror
functionality - 🔧 Enhanced Utilities: Additional error handling utilities like
ErrWith
trait - 🏗️ Consistent Patterns: Standardized error handling across the entire wTools ecosystem
- 🚀 Easy Migration: Drop-in replacement for existing
anyhow
/thiserror
usage - 🛡️ no_std Support: Works in
no_std
environments when needed
Quick Start
Installation
Basic Usage
Choose your approach based on your needs:
// For applications - flexible, untyped errors (anyhow-style)
use *;
// For libraries - structured, typed errors (thiserror-style)
use *;
use thiserror;
// For convenience - includes both
use *;
Core Concepts
1. Untyped Errors (Application-Focused)
Perfect for applications where you need flexible error handling without defining custom error types for every possible failure. This is a direct facade over anyhow
.
Key Features:
- Dynamic error handling with context
- Easy error chaining and reporting
- Rich context information
- Perfect for rapid prototyping and applications
use ;
Run this example:
2. Working with Context
Adding context to errors helps with debugging and user experience:
use ;
See the full runnable example in
examples/replace_anyhow.rs
.
3. Typed Errors (Library-Focused)
Ideal for libraries where you want to provide a clear, structured contract for possible errors. This is a facade over thiserror
.
Key Features:
- Structured error types with derive macros
- Clear error hierarchies
- Compile-time error checking
- Better API boundaries for library consumers
use Error;
use thiserror;
See the full runnable example in
examples/replace_thiserror.rs
.
4. Enhanced Error Context with ErrWith
The ErrWith
trait provides additional utilities for adding context to errors:
use ;
See the full runnable example in
examples/err_with_example.rs
.
5. Debug Assertions
Additional debugging utilities for development:
use ;
Examples
Basic Error Handling
use Result;
Using Both Typed and Untyped Errors
use *;
use thiserror;
// Typed error for library API
// Function returning typed error
// Function returning untyped error
Feature Flags
error_tools
supports granular feature control:
[]
= { = "0.26", = [ "error_typed" ] } # Only typed errors
# or
= { = "0.26", = [ "error_untyped" ] } # Only untyped errors
# or
= { = "0.26" } # Both (default)
Available Features:
default
- Enables botherror_typed
anderror_untyped
error_typed
- Enablesthiserror
integration for structured errorserror_untyped
- Enablesanyhow
integration for flexible errorsno_std
- Enablesno_std
supportuse_alloc
- Enables allocation support inno_std
environments
Migration Guide
From anyhow
Replace your anyhow
imports with error_tools::untyped
:
// Before
// use anyhow::{ Result, Context, bail, format_err };
// After
use ;
Everything else stays the same!
From thiserror
Add the explicit thiserror
import and use error_tools::typed
:
// Before
// use thiserror::Error;
// After
use Error;
use thiserror; // Required for derive macros
The derive macros work identically.
Complete Examples
Explore these runnable examples in the repository:
# Basic usage patterns
# Migration from anyhow
# Migration from thiserror
# Using the ErrWith trait
Best Practices
1. Choose the Right Error Style
- Applications: Use
untyped
errors for flexibility and rapid development - Libraries: Use
typed
errors for clear API contracts and better user experience - Mixed Projects: Use both as appropriate - they interoperate well
2. Error Context
Always provide meaningful context:
use ;
3. Error Hierarchies
For libraries, design clear error hierarchies:
use Error;
use thiserror;
// Define the individual error types
4. Dependency Access
When you need direct access to the underlying crates:
// Access the underlying crates if needed
// use error_tools::dependency::{ anyhow, thiserror };
// Or via the specific modules
use untyped; // Re-exports anyhow
use typed; // Re-exports thiserror
Integration with wTools Ecosystem
error_tools
is designed to work seamlessly with other wTools crates:
- Consistent Error Handling: All wTools crates use
error_tools
for unified error patterns - Cross-Crate Compatibility: Errors from different wTools crates integrate naturally
- Standardized Debugging: Common debugging utilities across the ecosystem
To add to your project
Try out from the repository
# Or try the specific examples