debug_err 0.1.0

A minimal error handling library.
Documentation
  • Coverage
  • 42.86%
    3 out of 7 items documented1 out of 2 items with examples
  • Size
  • Source code size: 6.82 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.47 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ericfindlay

An error handling library with under 50 lines of code and no dependencies, that keeps track of the source location of every error. DebugErr identifies the crate name, file name and line number of every error event.

The Basics

To build an error:

use debug_err::{DebugErr, src};

pub fn divide(num: f32, denom: f32) -> Result<f32, DebugErr> {
    if denom == 0.0 { Err(src!("Denominator must not be zero.")) }
    Ok(num / denom)
}

Coercing other error types to DebugErr

This extracts the message from another error type

use debug_err::{DebugErr, src};

let num: f32 = "abc".parse().map_err(|e| src!("Failed to parse with error '{e}'"))?;

Coercing DebugErr to other error types.

The cleanest method is just to directly use DebugErr in your library, and construct and return errors of this type, but if you still need to build your own error types then,

use {
    debug_err::{DebugErr, src},
    std::fmt,
};

#[derive(Clone, Debug)]
pub struct OwnError(DebugErr);

// Uses ``DebugErr``'s ``Display`` implementation.
impl fmt::Display for OwnError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

// This is used to coerce ``DebugErr`` into one's own type using the ``?`` operator,
// so that ``DebugErr`` doesn't have to be coerced explicitly.
impl From<DebugErr> for OwnError {
    fn from(debug: DebugErr) -> Self { OwnError(debug) }
}

// and then at the error construction site,

type Result<T> = std::result::Result<T, OwnError>;

pub fn divide(num: f32, denom: f32) -> Result<f32> {
    if denom == 0.0 { return Err(src!("Denominator must not be zero."))? }
    Ok(num / denom)
}

pub fn parse(num: &str) -> Result<f32> {
    let f: f32 = num.parse().map_err(|e| src!("Failed to parse '{num}' with error: {e}"))?;
    Ok(f)
}