Crates.io | conerror |
lib.rs | conerror |
version | |
source | src |
created_at | 2023-12-27 13:16:32.397274+00 |
updated_at | 2025-02-22 14:58:22.885292+00 |
description | Provides a macro that automatically adds context to errors |
homepage | |
repository | https://github.com/qtoolco/conerror |
max_upload_size | |
id | 1081656 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
conerror
is a Rust library designed to automatically add context to errors,
making it easier to trace and debug issues by including file names, line numbers,
and function names in error messages.
Here's a basic example demonstrating how to use the conerror macro to add context to errors:
use conerror::conerror;
fn main() {
if let Err(e) = run() {
println!("{}", e);
}
}
#[conerror]
fn run() -> conerror::Result<()> {
App.load_config()?;
Ok(())
}
struct App;
#[conerror]
impl App {
#[conerror]
fn load_config(&self) -> conerror::Result<Vec<u8>> {
let config = file_get_contents("non_exists_config.toml")?;
Ok(config)
}
}
#[conerror]
fn file_get_contents(path: &str) -> conerror::Result<Vec<u8>> {
fn read(path: &str) -> conerror::Result<Vec<u8>> {
Ok(std::fs::read(path)?)
}
read(path).map_err(|err| err.context(format!("Failed to read file {}", path)))
}
When the above example is run, it produces the following output:
Failed to read file non_exists_config.toml: No such file or directory (os error 2)
#0 src/main.rs:29 untitled::file_get_contents()
#1 src/main.rs:21 untitled::App::load_config()
#2 src/main.rs:11 untitled::run()