#anyhow #error #async #handling

async-anyhow-logger

An easy crate for catching anyhow errors from an asynchronous function, and passing them to your logger

1 unstable release

0.1.0 Feb 20, 2021

#1042 in Asynchronous


Used in peshming

MIT/Apache

6KB

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.

Dependencies

~0.8–1MB
~19K SLoC