use rootcause::{
Report, bail,
compat::{IntoRootcause, error_stack06::IntoErrorStack},
};
#[derive(Debug)]
struct ConnectionError;
impl core::fmt::Display for ConnectionError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("connection failed")
}
}
impl core::error::Error for ConnectionError {}
fn some_error_stack_function() -> Result<String, error_stack::Report<ConnectionError>> {
error_stack::bail!(ConnectionError);
}
fn rootcause_calls_error_stack() -> Result<(), Report> {
use rootcause::prelude::ResultExt;
let value = some_error_stack_function()
.into_rootcause()
.context("Failed to get value")?;
println!("Got value: {}", value);
Ok(())
}
fn some_rootcause_function() -> Result<String, Report> {
bail!("validation failed");
}
fn error_stack_calls_rootcause() -> Result<(), error_stack::Report<rootcause::compat::ReportAsError>>
{
use error_stack::ResultExt;
let value = some_rootcause_function()
.into_error_stack()
.attach("Failed to process data")?;
println!("Got value: {}", value);
Ok(())
}
fn rootcause_function_returning_error_stack()
-> Result<(), error_stack::Report<rootcause::compat::ReportAsError>> {
some_rootcause_function()?;
Ok(())
}
fn main() -> Result<(), error_stack::Report<rootcause::compat::ReportAsError>> {
println!("Rootcause ↔ error-stack Interoperability Examples\n");
println!("===========================================\n");
println!("=== Example 1: error-stack → Rootcause ===\n");
if let Err(e) = rootcause_calls_error_stack() {
println!("Error occurred:");
println!("{}\n", e);
}
println!("=== Example 2: Rootcause → error-stack ===\n");
if let Err(e) = error_stack_calls_rootcause() {
println!("Error occurred:");
println!("{}\n", e);
}
println!("=== Example 3: Automatic Conversion with ? ===\n");
if let Err(e) = rootcause_function_returning_error_stack() {
println!("Error occurred:");
println!("{}\n", e);
}
println!("===========================================");
println!("\nFor more examples, see:");
println!("- examples/anyhow_interop.rs - anyhow integration");
println!("- examples/eyre_interop.rs - eyre integration");
Ok(())
}