Crates.io | nuhound |
lib.rs | nuhound |
version | 0.1.2 |
source | src |
created_at | 2022-10-12 09:55:52.463726 |
updated_at | 2022-10-23 14:08:18.647564 |
description | Improve error handling capability |
homepage | https://github.com/converse99/nuhound |
repository | https://github.com/converse99/nuhound |
max_upload_size | |
id | 686144 |
size | 45,850 |
A Rust library for enhanced error tracking
Rust programmers often find the question mark operator invaluable in extracting values from Result and Option and immediately returning to the calling context in the case of an Err or None. This crate provides some enhancements to this functionality by:
disclose
feature that enhances error messages by incuding the filename, line
number and column number of the source file that caused the error. This functionality is
provided by the here! macro when the disclose
feature is enabled;Remember to add this to Cargo.toml:
[features]
# To help diagnose errors, use the disclose feature when compiling.
# This ensures that the source file name and line number are displayed
# when using the here! macro.
# example usage: cargo build --features=disclose
disclose = []
The following example shows how the here
macro is used to report an error but still retain
the underlying error or errors that can be displayed using the trace
method.
use nuhound::{Report, here, ResultExtension};
fn generate_error() -> Report<u32> {
let text = "NaN";
let value = text.parse::<u32>().report(|e| here!(e, "Oh dear - '{}' could not be \
converted to an integer", text))?;
Ok(value)
}
let result = generate_error();
match result {
Ok(_) => unreachable!(),
Err(e) => {
println!("Display the error:\n{e}\n");
println!("Or trace the error:\n{}\n", e.trace());
}
}
// This will emit:
// Display the error:
// Oh dear - 'NaN' could not be converted to an integer
//
// Or trace the error:
// 0: Oh dear - 'NaN' could not be converted to an integer
// 1: invalid digit found in string
//
// This will also show the name of the file causing the error
// and the line and column number if the code is compiled with
// the disclose feature enabled.
This project is licensed under either: