[−][src]Macro roa_core::throw
Throw an Err(Error).
throw!(status_code)will be expanded tothrow!(status_code, "")throw!(status_code, message)will be expanded tothrow!(status_code, message, true)throw!(status_code, message, expose)will be expanded toreturn Err(Error::new(status_code, message, expose));
Example
use roa_core::{App, throw}; use async_std::task::spawn; use http::StatusCode; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let (addr, server) = App::new(()) .gate_fn(|ctx, next| async move { next().await?; // throw unreachable!(); ctx.resp_mut().await.status = StatusCode::OK; Ok(()) }) .end(|_ctx| async { throw!(StatusCode::IM_A_TEAPOT, "I'm a teapot!"); // throw unreachable!() }) .run_local()?; spawn(server); let resp = reqwest::get(&format!("http://{}", addr)).await?; assert_eq!(StatusCode::IM_A_TEAPOT, resp.status()); assert_eq!("I'm a teapot!", resp.text().await?); Ok(()) }