errcraft 0.1.0

Beautiful, structured, and colorful error handling for Rust.
Documentation
//! Example demonstrating nested error chains.

use errcraft::ErrFrame;
use std::io;

fn deep_function() -> Result<(), io::Error> {
    Err(io::Error::new(
        io::ErrorKind::NotFound,
        "Configuration file not found",
    ))
}

fn middle_function() -> Result<(), ErrFrame> {
    deep_function().map_err(|e| {
        ErrFrame::new("Failed to load configuration")
            .context("layer", "middleware")
            .with_source(e)
    })
}

fn top_function() -> Result<(), ErrFrame> {
    middle_function().map_err(|e| {
        ErrFrame::new("Application initialization failed")
            .context("phase", "startup")
            .context("component", "config_loader")
            .with_source(e)
    })
}

fn database_error_chain() -> Result<(), ErrFrame> {
    let connection_err = io::Error::new(io::ErrorKind::ConnectionRefused, "Connection refused");

    let pool_err = ErrFrame::new("Failed to create connection pool")
        .context("max_connections", 10)
        .context("timeout_ms", 5000)
        .with_source(connection_err);

    Err(ErrFrame::new("Database initialization failed")
        .context("database", "postgresql")
        .context("host", "localhost:5432")
        .with_source(pool_err))
}

fn main() {
    println!("=== errcraft Nested Errors Example ===\n");

    // Example 1: Three-level error chain
    println!("Example 1: Three-level error chain");
    if let Err(e) = top_function() {
        e.eprint();
    }
    println!("\n{}\n", "=".repeat(60));

    // Example 2: Database connection chain
    println!("Example 2: Database error chain");
    if let Err(e) = database_error_chain() {
        e.eprint();
    }
    println!("\n{}\n", "=".repeat(60));

    // Example 3: Multiple context layers
    println!("Example 3: Rich context in nested errors");
    let api_error = io::Error::new(io::ErrorKind::TimedOut, "Request timed out");

    let err = ErrFrame::new("API request failed")
        .context("endpoint", "/api/v1/users")
        .context("method", "GET")
        .context("retry_count", 3)
        .context_text("Network timeout after 30 seconds")
        .with_source(api_error)
        .capture_backtrace();

    err.eprint();
}