Crates.io | async-anyhow-logger |
lib.rs | async-anyhow-logger |
version | 0.1.0 |
source | src |
created_at | 2021-02-20 18:03:41.873427 |
updated_at | 2021-02-20 18:03:41.873427 |
description | An easy crate for catching anyhow errors from an asynchronous function, and passing them to your logger |
homepage | |
repository | https://github.com/aspenluxxxy/async-anyhow-logger |
max_upload_size | |
id | 358111 |
size | 16,694 |
async-anyhow-logger
is a crate for easily logging errors that occur in asynchronous functions, such as those made via tokio::spawn
use anyhow::{Context, Result};
use async_anyhow_logger::catch_context;
pub async fn erroring_async_function() -> Result<()> {
Err(std::io::Error::new(std::io::ErrorKind::Other, "Oh no! An error!").into())
}
pub async fn multi_erroring_async_function() -> Result<()> {
erroring_async_function()
.await
.context("Oh no! Another error!")
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
tokio::spawn(catch_context(
"my async function errored",
erroring_async_function(),
));
tokio::spawn(catch_context(
"more complicated async function errored",
multi_erroring_async_function(),
));
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
This example is also located in examples/basic.rs, and can be can with cargo run --example basic
.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.