error_status 0.1.0

Model common error context with HTTP 4xx and 5xx code
Documentation
  • Coverage
  • 7.84%
    4 out of 51 items documented1 out of 5 items with examples
  • Size
  • Source code size: 25.51 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.43 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • kanru/error_status
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • kanru

error_status

License: MIT OR Apache-2.0 crates.io

A lightweight error handling library for Rust that combines HTTP-style error statuses with contextual information.

Features

  • HTTP-inspired Error Status: Wrap errors with semantic status codes similar to HTTP status codes
  • Context-Rich Errors: Add meaningful context strings to your errors for better debugging
  • Result Extension Trait: Convenient methods to construct error statuses from Result type

Installation

Add this to your Cargo.toml:

[dependencies]
error_status = "0.1.0";

Quick Start

use std::io::{self, ErrorKind};

use anyhow::Result;
use error_status::ResultExt;

fn find_file() -> Result<(), io::Error> {
    Err(ErrorKind::NotFound.into())
}

fn main() -> Result<()> {
    find_file()
        .not_found("Failed to read file")
        .internal_error("Config file is not available")?;
    Ok(())
}

Usage

The library provides a ResultExt trait that extends Result with methods corresponding to common error scenarios:

  • not_found(): For missing resource errors
  • internal_error(): For internal system errors
  • bad_request(): For validation failures
  • And more...

Each method accepts a context string that provides additional information about the error. There's also a corresponding _lazy() version for context builder.

Error Handling Best Practices

  • Use semantic status codes to categorize errors appropriately
  • Provide meaningful context messages for better error tracking
  • Implement comprehensive logging throughout your codebase
  • Use the error handling chain to provide multiple layers of context

License

MIT OR Apache-2.0

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.