error_status

Crates.ioerror_status
lib.rserror_status
version0.1.0
created_at2025-02-18 12:35:30.360379+00
updated_at2025-02-18 12:35:30.360379+00
descriptionModel common error context with HTTP 4xx and 5xx code
homepage
repositoryhttps://github.com/kanru/error_status
max_upload_size
id1559857
size26,012
Kan-Ru Chen (kanru)

documentation

README

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.

Commit count: 2

cargo fmt