macro_rules! ctx_async {
($fut:expr, $msg:literal $(,)?) => { ... };
($fut:expr, $fmt:literal, $($arg:tt)* $(,)?) => { ... };
}Expand description
Attaches context to a future’s error with format string support.
This macro provides a convenient shorthand for attaching context to
async operations, similar to how context! works for sync code.
§Syntax
ctx_async!(future, "literal message")- Static messagectx_async!(future, "format {}", arg)- Formatted message (formatting is deferred until error)
§Examples
use error_rail::prelude_async::*;
#[derive(Debug)]
struct User;
#[derive(Debug)]
struct Profile;
#[derive(Debug)]
struct ApiError;
async fn fetch_user(_id: u64) -> Result<User, ApiError> {
Err(ApiError)
}
async fn fetch_profile(_id: u64) -> Result<Profile, ApiError> {
Err(ApiError)
}
async fn example(id: u64) -> BoxedResult<User, ApiError> {
// Static message
let user = ctx_async!(fetch_user(id), "fetching user")
.await
.map_err(Box::new)?;
// With formatting (formatting is deferred until error)
let _profile = ctx_async!(fetch_profile(id), "fetching profile for user {}", id)
.await
.map_err(Box::new)?;
Ok(user)
}