bigerror 0.12.0

handle big errors ¯\_(ツ)_/¯
Documentation

Enhanced error handling library built on top of [error-stack][error_stack].

bigerror provides ergonomic error handling adding out of the box functionality to [error-stack][error_stack] for common scenarios.

Key Features

  • Useful context attachments - Key-value pairs, field status, type information
    • see [kv!], [ty!], [expect_field!]
  • Pre-defined contexts: [NotFound], [ParseError], [Timeout], etc.
  • no_std support: Works in embedded and constrained environments

Quick Start

use bigerror::{ThinContext, Report, expect_field, IntoContext, ResultIntoContext, NotFound};

// Define your error type
#[derive(ThinContext)]
struct MyError;

fn parse_number(input: &str) -> Result<i32, Report<MyError>> {
    // Use context conversion for error handling
    let num: i32 = input.parse()
        .into_ctx::<MyError>()?; // `::<MyError>` can be omitted

    Ok(num)
}

// Example with `expect_field` for optional values
fn get_config_value() -> Result<&'static str, Report<NotFound>> {
    let config = Some("production");
    expect_field!(config).into_ctx()
}

Attachments

Attach contextual information to errors using various attachment types:

use bigerror::{ThinContext, kv, ty, KeyValue};

#[derive(ThinContext)]
struct MyError;

// Key-value attachments
let error = MyError::attach_kv("user_id", 42);

// Type-value attachments
let data = vec![1, 2, 3];
let error = MyError::attach_kv(ty!(Vec<i32>), data.len());

#[derive(Debug, Clone, Eq, PartialEq)]
struct Username(String);
impl std::fmt::Display for Username {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

let username = String::from("alice");
assert_eq!(kv!(username.clone()), KeyValue("username", String::from("alice")));
let error = MyError::attach(kv!(username.clone())); // "username": "alice"

let username = Username(username);
assert_eq!(format!("{}", kv!(ty: username)), "<Username>: alice");

Feature Flags

  • std (default) - Standard library support
  • backtrace (default) - Backtrace support
  • tracing - Integration with the tracing ecosystem
  • serde - Serialization support
  • anyhow - Compatibility with anyhow
  • eyre - Compatibility with eyre

Pre-defined Error Contexts

Common error contexts are provided out of the box:

  • [NotFound] - Missing resources, failed lookups
  • [ParseError] - Parsing and deserialization failures
  • [Timeout] - Operations that exceed time limits
  • [InvalidInput] - Validation and input errors
  • [ConversionError] - Type conversion failures

See the [context] module for the complete list.