Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 20, 2021 10:12
Show Gist options
  • Save rust-play/28fa7cbd18b08560e61c28ca02ee4bb4 to your computer and use it in GitHub Desktop.
Save rust-play/28fa7cbd18b08560e61c28ca02ee4bb4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![feature(backtrace)]
#![feature(backtrace_frames)]
use std::{backtrace::Backtrace, error::Error};
use thiserror::Error as ThisError;
#[derive(ThisError, Debug)]
#[error("MyError")]
struct MyError {
backtrace: std::backtrace::Backtrace,
}
#[derive(ThisError, Debug)]
#[error("OtherError")]
struct OtherError {
backtrace: std::backtrace::Backtrace,
}
#[derive(ThisError, Debug)]
enum AllErrors {
#[error("E")]
E(#[from] MyError),
#[error("O")]
O(#[from] OtherError, Backtrace),
}
fn main() {
let my = MyError {
backtrace: std::backtrace::Backtrace::capture(), // Backtrace location A
};
assert!(my.backtrace().is_some());
let other = OtherError {
backtrace: std::backtrace::Backtrace::capture(), // Backtrace location B
};
assert!(other.backtrace().is_some());
let all_my = AllErrors::from(my);
let all_other = AllErrors::from(other); // Implicit Backtrace C
println!("My my {:#?}", all_my.backtrace()); // -> None
println!("My my source {:#?}", all_my.source().unwrap().backtrace()); // -> Backtrace A
println!("Other my {:#?}", all_other.backtrace()); // -> Backtrace B
println!(
"Other my source {:#?}",
all_other.source().unwrap().backtrace() // Backtrace B
);
// Inconsistency:
// Option 1 : Always return inner backtrace
// all_my.backtrace() should return Backtrace A
// all_other.backtrace() should return Backtrace B // as
// Option 2: Always deliver own backtrace
// all_my.backtrace() should return None
// all_other.backtrace() should return Backtrace C ( where all_other was created )
// The prefered option is Option 2 as currently there is no way to access backtrace C
// using the backtrace interface
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment