Skip to content

Instantly share code, notes, and snippets.

@VendettaReborn
Created February 15, 2025 12:37
Show Gist options
  • Save VendettaReborn/4876cc32aacb149b93c44bcc13c62f9d to your computer and use it in GitHub Desktop.
Save VendettaReborn/4876cc32aacb149b93c44bcc13c62f9d to your computer and use it in GitHub Desktop.
error backtrace stack in thiserror
#![feature(error_generic_member_access)]
#[derive(thiserror::Error, Debug)]
pub enum RustError {
#[error("Java error: {source}")]
Java {
#[from]
#[backtrace]
source: JavaError,
},
}
#[derive(thiserror::Error, Debug)]
pub enum JavaError {
#[error("Python error: {source}")]
IO {
#[from]
source: PythonError,
},
}
#[derive(thiserror::Error, Debug)]
pub enum PythonError {
#[error("Io error: {source}")]
IO {
#[from]
source: std::io::Error,
backtrace: std::backtrace::Backtrace,
},
}
fn fn1() -> std::result::Result<(), RustError> {
fn2()?;
Ok(())
}
fn fn2() -> std::result::Result<(), JavaError> {
fn3()?;
Ok(())
}
fn fn3() -> std::result::Result<(), PythonError> {
Err(std::io::Error::new(std::io::ErrorKind::Other, "error"))?
}
fn main() {
let res = fn1();
println!("{:?}", res);
}
/*
example output:
Err(Java { source: IO { source: IO { source: Custom { kind: Other, error: "error" }, backtrace: Backtrace [{ fn: "<demo_thiserror::PythonError as core::convert::From<std::io::error::Error>>::from", file: "./examples/examples/demo_thiserror.rs", line: 22 }, { fn: "<core::result::Result<T,F> as core::ops::try_trait::FromResidual<core::result::Result<core::convert::Infallible,E>>>::from_residual", file: "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs", line: 2014 }, { fn: "demo_thiserror::fn3", file: "./examples/examples/demo_thiserror.rs", line: 43 }, { fn: "demo_thiserror::fn2", file: "./examples/examples/demo_thiserror.rs", line: 38 }, { fn: "demo_thiserror::fn1", file: "./examples/examples/demo_thiserror.rs", line: 33 }, { fn: "demo_thiserror::main", file: "./examples/examples/demo_thiserror.rs", line: 47 }, { fn: "core::ops::function::FnOnce::call_once", file: "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs", line: 250 }, { fn: "std::sys::backtrace::__rust_begin_short_backtrace", file: "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs", line: 152 }, { fn: "std::rt::lang_start::{{closure}}", file: "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs", line: 194 }, { fn: "core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/core/src/ops/function.rs", line: 284 }, { fn: "std::panicking::try::do_call", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/panicking.rs", line: 587 }, { fn: "std::panicking::try", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/panicking.rs", line: 550 }, { fn: "std::panic::catch_unwind", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/panic.rs", line: 358 }, { fn: "std::rt::lang_start_internal::{{closure}}", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/rt.rs", line: 163 }, { fn: "std::panicking::try::do_call", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/panicking.rs", line: 587 }, { fn: "std::panicking::try", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/panicking.rs", line: 550 }, { fn: "std::panic::catch_unwind", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/panic.rs", line: 358 }, { fn: "std::rt::lang_start_internal", file: "/rustc/6067b36314ab5eb2eb47cecc464545ba58e1ad24/library/std/src/rt.rs", line: 159 }, { fn: "std::rt::lang_start", file: "/root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs", line: 193 }, { fn: "main" }, { fn: "__libc_start_main" }, { fn: "_start" }] } } })
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment