#failure #error #rocket #api

rocket_failure_errors

rocket_failure的Error结构体

1个不稳定版本

0.2.0 2019年7月22日

#43 in #failure

Download history 92/week @ 2024-03-31 66/week @ 2024-04-07 64/week @ 2024-04-14 78/week @ 2024-04-21 105/week @ 2024-04-28 74/week @ 2024-05-05 68/week @ 2024-05-12 81/week @ 2024-05-19 84/week @ 2024-05-26 70/week @ 2024-06-02 75/week @ 2024-06-09 64/week @ 2024-06-16 49/week @ 2024-06-23 44/week @ 2024-06-30 43/week @ 2024-07-07 66/week @ 2024-07-14

211 每月下载量
3 个crate中(2个直接使用) 使用

MIT/Apache

3KB

rocket_failure

为rocket应用提供语义错误处理。

要在您的服务器中启用此crate,请在您的 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();
}

您可以使用以下命令运行此示例

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

如果您想在API客户端中使用 ApiResult<T> 类型来消费API,省略 with-rocket 功能

rocket_failure = "0.1"

许可证

rocket_failure根据您选择,许可如下

依赖

~0.4–1MB
~23K SLoC