#error #simple-error

simple-error

A simple error type backed by a string

17 releases

Uses old Rust 2015

0.2.3 Jan 14, 2021
0.2.2 Aug 16, 2020
0.2.1 Jul 3, 2019
0.1.13 Dec 7, 2018
0.1.1 Mar 24, 2016

#90 in Rust patterns

Download history 3629/week @ 2022-03-09 3865/week @ 2022-03-16 4371/week @ 2022-03-23 5530/week @ 2022-03-30 5879/week @ 2022-04-06 4507/week @ 2022-04-13 4877/week @ 2022-04-20 5014/week @ 2022-04-27 5634/week @ 2022-05-04 5407/week @ 2022-05-11 4447/week @ 2022-05-18 4538/week @ 2022-05-25 5851/week @ 2022-06-01 5921/week @ 2022-06-08 6027/week @ 2022-06-15 4846/week @ 2022-06-22

23,590 downloads per month
Used in 79 crates (69 directly)

MIT/Apache

17KB
178 lines

simple-error

crates.io Documentation Build Status Coverage Status MSRV

simple-error is a Rust library that provides a simple Error type backed by a String. It is best used when all you care about the error is an error string.

Documentation

Usage

To use simple-error, first add this to your Cargo.toml:

[dependencies]
simple-error = "0.2"

Then add this to your crate root:

#[macro_use]
extern crate simple_error;

use simple_error::SimpleError;

Or you can skip the extern crate and just import relevant items you use if you are on 2018 edition or beyong.

Now you can use simple-error in different ways:

You can use it simply as a string error type:

fn do_foo() -> Result<(), SimpleError> {
    Err(SimpleError::new("cannot do foo"))
}

You can use it to replace all error types if you only care about a string description:

fn do_bar() -> Result<(), SimpleError> {
    Err(SimpleError::from(std::io::Error(io::ErrorKind::Other, "oh no")))
}

Or you can chain all the errors, and get a complete error description at the top level:

fn find_tv_remote() -> Result<(), SimpleError> {
    try_with!(std::fs::File::open("remotefile"), "failed to open remote file");
    Ok(())
}

fn turn_on_tv() -> Result<(), std::io::Error> {
    Ok(())
}

fn watch_tv() -> Result<(), SimpleError> {
    try_with!(find_tv_remote(), "tv remote not found");
    try_with!(turn_on_tv(), "cannot turn on tv");
    Ok(())
}

fn study() -> Result<(), SimpleError> {
    Ok(())
}

fn run() -> Result<(), SimpleError> {
    try_with!(study(), "cannot study");
    try_with!(watch_tv(), "cannot watch tv");
    Ok(())
}

fn main() {
    if let Err(e) = run() {
        println!("{}", e);
    }
}
// This prints out "cannot watch tv, tv remote not found, failed to open remote file, Text file busy" if the error is text file busy.

No runtime deps