#rocket #failure #errors #api

rocket_failure

Failure like error handling for rocket

4 releases

0.2.0 Jul 22, 2019
0.1.2 Feb 26, 2019
0.1.1 Dec 9, 2018
0.1.0 Dec 9, 2018

#677 in Rust patterns

Download history 14/week @ 2021-04-07 21/week @ 2021-04-14 15/week @ 2021-04-21 17/week @ 2021-04-28 14/week @ 2021-05-05 21/week @ 2021-05-12 14/week @ 2021-05-19 7/week @ 2021-05-26 9/week @ 2021-06-02 11/week @ 2021-06-09 9/week @ 2021-06-16 14/week @ 2021-06-23 18/week @ 2021-06-30 17/week @ 2021-07-07 17/week @ 2021-07-14 6/week @ 2021-07-21

91 downloads per month
Used in sn0int-registry

MIT/Apache

16KB
289 lines

rocket_failure

Semantic error handling for rocket applications.

To enable this crate in your server add this line to your Cargo.toml:

rocket_failure = { version="0.1", features = ["with-rocket"] }
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_failure;

use rocket_failure::errors::*;
use std::fs;

#[get("/<file>")]
fn index(file: String) -> ApiResult<Vec<u8>> {
    if !file.chars().all(|c| char::is_alphanumeric(c) || c == '-' || c == '.') {
        bad_request!("file contains forbidden characters")
    }

    // if this returns an Err(_), return a standard 404
    let content = fs::read(&file)
        .not_found()?;

    // detailed errors are hidden by default
    // we can publish the actual error if we want to
    /*
    let content = fs::read(&file)
        .not_found()
        .publish_error()?;
    */

    // or we can set a public error while preserving the actual error
    /*
    let content = fs::read(&file)
        .not_found()
        .public_context("That didn't work")?;
    */

    Ok(content)
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();
}

You can run this example with:

cargo +nightly run --example fileserver --features=with-rocket

With you want to use the ApiResult<T> type in your api client to consume the api, omit the with-rocket feature:

rocket_failure = "0.1"

License

rocket_failure is licensed under either of the following, at your option:

Dependencies

~8.5MB
~190K SLoC