Semantic error handling for rocket applications.
To enable this crate in your server add this line to your Cargo.toml
:
toml
rocket_failure = { version="0.1", features = ["with-rocket"] }
```rust
use rocket_failure::errors::*; use std::fs;
fn index(file: String) -> ApiResult
// 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:
toml
rocket_failure = "0.1"
rocket_failure is licensed under either of the following, at your option: