#rocket #failure #errors #api

rocket_failure_errors

Error structs for rocket_failure

1 unstable release

0.2.0 Jul 22, 2019

#9 in #failure

Download history 59/week @ 2021-04-06 38/week @ 2021-04-13 47/week @ 2021-04-20 38/week @ 2021-04-27 51/week @ 2021-05-04 108/week @ 2021-05-11 76/week @ 2021-05-18 85/week @ 2021-05-25 45/week @ 2021-06-01 50/week @ 2021-06-08 39/week @ 2021-06-15 87/week @ 2021-06-22 44/week @ 2021-06-29 35/week @ 2021-07-06 45/week @ 2021-07-13 46/week @ 2021-07-20

311 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

2KB

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

~0.6–1.2MB
~28K SLoC