[][src]Macro roa_core::throw

macro_rules! throw {
    ($status_code:expr) => { ... };
    ($status_code:expr, $message:expr) => { ... };
    ($status_code:expr, $message:expr, $expose:expr) => { ... };
}

Throw an Err(Error).

  • throw!(status_code) will be expanded to throw!(status_code, "")
  • throw!(status_code, message) will be expanded to throw!(status_code, message, true)
  • throw!(status_code, message, expose) will be expanded to return 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(())
}