Crate neuer_error

Crate neuer_error 

Source
Expand description

The error that can be whatever you want (it is Mr. Neuer). In every case (hopefully). NO AI SLOP!

An error handling library designed to be:

  • Useful in both libraries and applications, containing human and machine information.
  • Ergonomic, low-boilerplate and comfortable, while still adhering best-practices and providing all necessary infos.
  • Flexible in interfacing with other error handling libraries.

§Features/Highlights

  • Most importantly: error messages, that are helpful for debugging. By default it uses source locations instead of backtraces, which is often easier to follow, more efficient and works without debug info.
  • Discoverable, typed context getters without generic soup, type conversions and conflicts.
  • Works with std and no-std, but requires a global allocator.
  • Compatible with non-Send/Sync environments, but also with Send/Sync environments (per feature flag).
  • Out of the box source error chaining.

§Why a new (German: neuer) error library?

Long story, you can view it here.

TLDR: I wasn’t satisfied with my previous approach and existing libraries I know. And I was inspired by a blog post to experiment myself with error handling design.

§Usage

The best way to see how to use it for your use-case is to check out the examples. Nevertheless, here is a quick demo:

// In library/module:
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Retryable { No, Yes }

// Provide discoverable, typed information for library users.
provided_attachments!(
  retryable(single: Retryable) -> bool {
    |retryable| matches!(retryable, Some(Retryable::Yes))
  };
);

fn do_something_internal() -> Result<()> {
  Err(CtxError::new("Error occurred internally")
    .attach(Retryable::No))
}

pub fn do_something() -> Result<()> {
  do_something_internal().context("Operation failed")
}

// In consumer/application:
fn main() {
  match do_something() {
    Ok(()) => {}
    Err(err) if err.retryable() => {
      eprintln!("Retryable error");
    }
    Err(_) => {
      eprintln!("Non-retryable error");
    }
  }
}

Run cargo add neuer-error to add the library to your project.

§Feature Flags

default -> std, send, sync: Default selected features. Deactivate with default-features=false.

std (default): Enables use of std. Provides interaction with ExitCode termination.

send (default): Requires all contained types to be Send, so that CtxError is also Send.

sync (default) -> send: Requires all contained types to be Sync, so that CtxError is also Sync.

Modules§

traits
All traits that need to be in scope for comfortable usage.

Macros§

provided_attachments
Create a helper trait CtxErrorAttachments that is implemented for CtxError, which allows to directly retrieve your attachments. You can modify visibility and name by re-exporting via pub use if needed.

Structs§

CtxError
Generic rich error type for use within Results, for libraries and applications.
CtxErrorImpl
Inner implementation of CtxError that implements Error.

Traits§

ConvertOption
Helper on Options for conversion to our Results.
ConvertResult
Helper on Results with external Errors for conversion to our CtxError.
CtxResultExt
Helper on our Results for context addition and modification.
ResultExt
Helpers on Results.

Functions§

Ok
Create a Result::Ok value with CtxError as given error type.

Type Aliases§

Result
Result type alias using the crate’s CtxError type.