Crate exn

Source
Expand description

A context-aware concrete Error type built on std::error::Error

§Examples

use exn::Exn;
use exn::Result;
use exn::ResultExt;
// using `thiserror` is unnecessary but convenient
use thiserror::Error;

// Errors can enumerate variants users care about
// but notably don't need to chain source/inner error manually.
#[derive(Debug, Error)]
enum AppError {
    #[error("serious app error: {consequences}")]
    Serious { consequences: &'static str },
    #[error("trivial app error")]
    Trivial,
}

type AppResult<T> = Result<T, AppError>;

// Errors can also be a plain `struct`, somewhat like in `anyhow`.
#[derive(Debug, Error)]
#[error("logic error")]
struct LogicError;

type LogicResult<T> = Result<T, LogicError>;

fn do_logic() -> LogicResult<()> {
    Ok(())
}

fn main() -> AppResult<()> {
    // `error-stack` requires developer to properly handle
    // changing error contexts
    do_logic().or_raise(|| AppError::Serious {
        consequences: "math no longer works",
    })?;

    Ok(())
}

Macros§

bail
Creates an Exn and returns it as Result.
ensure
Ensures $cond is met; otherwise return an error.

Structs§

Exn
An exception type that can hold an error tree and additional context.
ExnView
An immutable view of an exception.

Traits§

ContextBound
A trait to bound the context type of Exn.
ErrorBound
A trait to bound the error type of Exn.
IntoExn
A trait to convert a value into an Exn (exception) type.
ResultExt
An extension trait for Result to provide context information on Exns.
Visitor
A trait for visiting an exception.

Type Aliases§

Result
A reasonable return type to use throughout an application.