use rootcause::{
ReportRef,
hooks::{Hooks, context_formatter::ContextFormatterHook},
markers::{Dynamic, Local, Uncloneable},
prelude::*,
};
use rootcause_internals::handlers::{ContextFormattingStyle, FormattingFunction};
async fn make_request(url: &str) -> Result<String, Report> {
let response = reqwest::get(url).await?;
let body = response.text().await?;
Ok(body)
}
struct ReqwestErrorFormatter;
impl ContextFormatterHook<reqwest::Error> for ReqwestErrorFormatter {
fn preferred_context_formatting_style(
&self,
_report: ReportRef<'_, Dynamic, Uncloneable, Local>,
report_formatting_function: FormattingFunction,
) -> ContextFormattingStyle {
ContextFormattingStyle {
function: report_formatting_function,
follow_source: true,
follow_source_depth: None,
}
}
}
#[tokio::main]
async fn main() {
println!("Without source following:\n");
let result1 = make_request("https://this-domain-definitely-does-not-exist-12345.com")
.await
.context("Failed to fetch user data");
if let Err(err) = result1 {
println!("{err}\n");
}
println!("With source following:\n");
Hooks::new()
.context_formatter::<reqwest::Error, _>(ReqwestErrorFormatter)
.install()
.expect("Failed to install hooks");
let result2 = make_request("https://this-domain-definitely-does-not-exist-12345.com")
.await
.context("Failed to fetch user data");
if let Err(err) = result2 {
println!("{err}\n");
}
}