Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 1.2 KB

File metadata and controls

62 lines (45 loc) · 1.2 KB

Basic Async Example

Simple demonstration of using masterror in async Rust code with tokio.

Features

  • Async Error Handling - Using ? operator in async functions
  • Timeout Handling - Converting tokio timeout errors to AppError
  • Error Propagation - Clean error propagation through async call chains
  • Result Types - Using AppResult<T> for async operations

Running

cd examples/basic-async
cargo run

Key Concepts

Async Functions with AppError

async fn fetch_data(id: u64) -> Result<String, AppError> {
    if id == 0 {
        return Err(AppError::validation("ID cannot be zero"));
    }

    Ok(format!("Data for ID {}", id))
}

Timeout Error Handling

use tokio::time::{timeout, Duration};

let result = timeout(
    Duration::from_secs(5),
    fetch_data(123)
).await?; // Converts Elapsed to AppError::Timeout

Error Propagation

async fn process() -> Result<(), AppError> {
    let data = fetch_data(123).await?;
    let result = process_data(&data).await?;
    save_result(result).await?;
    Ok(())
}

License

MIT