#error-string #create #object

string-error

A minimal rust library to create errors out of strings

1 unstable release

Uses old Rust 2015

0.1.0 Oct 8, 2017

#2376 in Rust patterns

Download history 190/week @ 2023-12-18 81/week @ 2023-12-25 66/week @ 2024-01-01 288/week @ 2024-01-08 304/week @ 2024-01-15 269/week @ 2024-01-22 263/week @ 2024-01-29 356/week @ 2024-02-05 373/week @ 2024-02-12 274/week @ 2024-02-19 276/week @ 2024-02-26 280/week @ 2024-03-04 289/week @ 2024-03-11 378/week @ 2024-03-18 525/week @ 2024-03-25 530/week @ 2024-04-01

1,744 downloads per month
Used in 8 crates (7 directly)

Apache-2.0

6KB
62 lines

string-error

This crate provides a simple way to use a string as an error trait object, i.e. Box<std::error::Error>.

If you need more sophisticated error handling, you should consider error-chain, which also provides functionality to create simple errors from Strings.

Compatibility

This crate works with Stable Rust (tested with 1.20.0) and has no dependencies.

License

Written by Ulrich Rhein, licensed under the Apache License 2.0.

See COPYRIGHT and LICENSE for details.

Usage

In your Cargo.toml:

[dependencies]
string-error = "0.1.0"

In your code:

extern crate string_error;

use std::error::Error;
use string_error::{into_err, new_err, static_err};

static ERROR_MESSAGE : &'static str = "This is a constant error message";

fn use_static_err() -> Result<(), Box<Error>> {
    // creates an error from a static str
    Err(static_err(ERROR_MESSAGE))
}

fn use_new_err() -> Result<(), Box<Error>> {
    let x = String::from("Create an error from an owned string.");
    Err(new_err(&x)) // copies x
}

fn use_into_err() -> Result<(), Box<Error>> {
    let x = String::from("Create an error from an owned string.");
    Err(into_err(x)) // takes ownership of x
}

No runtime deps