async-anyhow-logger

Crates.ioasync-anyhow-logger
lib.rsasync-anyhow-logger
version0.1.0
sourcesrc
created_at2021-02-20 18:03:41.873427
updated_at2021-02-20 18:03:41.873427
descriptionAn easy crate for catching anyhow errors from an asynchronous function, and passing them to your logger
homepage
repositoryhttps://github.com/aspenluxxxy/async-anyhow-logger
max_upload_size
id358111
size16,694
Lucy (Absolucy)

documentation

README

async-anyhow-logger

async-anyhow-logger is a crate for easily logging errors that occur in asynchronous functions, such as those made via tokio::spawn

Examples

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.

License

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.

Commit count: 0

cargo fmt