#error #error-handling #local #fatal #result #nightly #enums

no-std woah

A Result type that differentiates between local errors (which can be handled) and fatal errors (which cannot)

10 unstable releases (3 breaking)

0.4.5 May 25, 2022
0.4.4 May 24, 2022
0.3.0 May 22, 2022
0.2.0 Aug 3, 2021
0.1.9 Sep 2, 2020

#843 in Rust patterns

40 downloads per month

MIT/Apache

72KB
1K SLoC

woah

Crates.io Crates.io

A Result that separates local errors you can handle from fatal errors you can't.

Sometimes you want to be able to pass some errors upward without handling them, while conveniently handling other kinds of errors at a local level. You might try to do this with manual pattern-matching on a Result, or by constructing a Result<Result<T, LocalError>, FatalError>. Instead, you can use woah!

Example

use woah::prelude::*;
use rand::prelude::*;

fn main() {
    match get_data() {
        Success(data) => println!("{}", data),
        LocalErr(e) => eprintln!("error: {:?}", e),
        FatalErr(e) => eprintln!("error: {:?}", e),
    }
}

/// Get data from an HTTP API.
fn get_data() -> Result<String, LocalError, FatalError> {
    match do_http_request()? {
        Ok(data) => Success(data),
        Err(e) => {
            eprintln!("error: {:?}... retrying", e);
            get_data()
        }
    }
}

/// Make an HTTP request.
///
/// This is simulated with randomly returning either a time out
/// or a request failure.
fn do_http_request() -> Result<String, LocalError, FatalError> {
    if random() {
        LocalErr(LocalError::RequestTimedOut)
    } else {
        FatalErr(FatalError::RequestFailed)
    }
}

/// Errors which can be handled.
#[derive(Debug)]
enum LocalError {
    RequestTimedOut,
}

/// Errors which can't be handled.
#[derive(Debug)]
enum FatalError {
    RequestFailed,
}

Use

woah can be used on stable Rust, but it's best when used on nightly.

On nightly, with the nightly feature enabled, you get access to the Try trait which allows use of the ? operator to propagate fatal errors. On stable, you have to convert to and from a std::result::Result to use the ? operator, which is less convenient.

Features

Feature Name Default? Purpose
nightly No Lets you use the ? operator with woah::Result, and adds some other convenience trait impls based on unstable APIs in std.
std Yes Uses std for imports, adds the Termination and ExitCode APIs, and if either is turned on, turns on std for either as well.
either Yes Adds methods to woah::Result for working with Either<LocalErr, FatalErr>
serde No Implements Serialize and Deserialize for woah::Result

License

woah is dual-licensed MIT or Apache 2.0.

Dependencies

~220KB