A comprehensive, developer-focused error handling infrastructure built on top of thiserror
, providing standardized error messages, common error patterns, and ergonomic helper functions.
This crate provides a "batteries-included" approach to error handling with these core principles:
Error messages provide technical context for developers, not end-user UI text.
Uses thiserror::Error
derive macro for compile-time checking, pattern matching, and IDE support.
Explicit, debuggable functions instead of complex macros:
// Clear and debuggable โจ
CommonError::io_error("Failed to read config file")
// Instead of macro magic ๐ชโ
error!("Failed to read config file")
Error messages defined as constants in a dedicated messages
module for consistency and maintainability.
// Add to Cargo.toml
[dependencies]
error-kit = "0.1.0"
use error_kit::CommonError;
// Using helper functions ๐ง
let io_err = CommonError::io_error("Failed to read config file");
let filename_err = CommonError::filename_error();
// Using predefined message constants ๐
let timeout_err = CommonError::Timeout; // Uses messages::TIMEOUT_EXCEEDED
// Simple error handling ๐ฏ
let result: Result<Data, CommonError> = some_operation();
match result {
Err(e) => eprintln!("Error: {}", e),
Ok(data) => process_data(data),
}
// Or pattern matching when you need specific handling
match result {
Err(CommonError::Io(_)) => handle_io_error(),
Err(CommonError::Timeout) => retry_operation(),
Err(CommonError::FilenameError) => handle_filename_issue(),
Err(e) => eprintln!("Error: {}", e),
Ok(data) => process_data(data),
}
The error-kit
crate is organized into focused modules:
messages
: Centralized error message constantsconstructors
: Ergonomic constructor functionstypes
: Main error enum withthiserror
integration
use error_kit::{
CommonError, // Main error type ๐ฏ
messages, // Message constants ๐
constructors, // Helper constructor functions ๐
};
- Library Development: Structured error types with consistent messaging
- Application Development: Standardized error handling with reduced boilerplate
- Error Infrastructure: Foundation for domain-specific error crates and company standards
This crate aims to become the go-to foundation for error handling, providing:
- Domain-specific error modules (network, filesystem, parsing, etc.)
- Error reporting utilities and formatters
- Integration helpers for popular crates
- Best practice examples and patterns
- Community-driven error message standards
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.