-
-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use backtrace after stabilisation? #262
Comments
Which compiler version do you use? Backtrace is stabilized, but still only on nightly - it will arrive on the stable branch in Rust 1.65. |
I'm using nightly, where it's currently stable. |
I guess one would need to use the Provider API. |
I don't get what this has to do with me printing a backtrace. |
It should still work as documented: fn main() {
let error = anyhow::Error::msg("huh");
println!("{:?}", error);
} $ RUST_LIB_BACKTRACE=1 cargo run
huh
Stack backtrace:
0: anyhow::error::<impl anyhow::Error>::msg
at anyhow-1.0.64/src/error.rs:83:36
1: repro::main
at ./src/main.rs:2:17
2: core::ops::function::FnOnce::call_once
at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/core/src/ops/function.rs:248:5
…
13: std::rt::lang_start_internal
at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs:148:20
14: std::rt::lang_start
at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs:165:17
…
18: _start or using Backtrace [
{ fn: "anyhow::error::<impl anyhow::Error>::msg", file: "anyhow-1.0.64/src/error.rs", line: 83 },
{ fn: "testing::main", file: "./src/main.rs", line: 2 },
{ fn: "core::ops::function::FnOnce::call_once", file: "/rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/core/src/ops/function.rs", line: 248 },
…
{ fn: "std::rt::lang_start_internal", file: "/rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs", line: 148 },
{ fn: "std::rt::lang_start", file: "/rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs", line: 165 },
…
{ fn: "_start" },
] |
Be mindful that a specific |
Hey so I'm not home right now so I can't quite test this. But my code doesn't just println the error. I manually unwind the context to print it how I want. I would like some way to also print the backtrace manually. |
Then #262 (comment) is the answer to that. For example using https://doc.rust-lang.org/nightly/core/any/fn.request_ref.html: #![feature(provide_any)]
use std::any;
use std::backtrace::Backtrace;
fn main() {
let error = anyhow::Error::msg("huh");
println!("{:#?}", error.backtrace());
for cause in error.chain().skip(1) {
if let Some(backtrace) = any::request_ref::<Backtrace>(cause) {
println!("{:#?}", backtrace);
}
}
} |
I can't quite figure out how to use backtrace now that it's stable.
The backtrace method is gone from the error type, however looking at the docs it says:
This seems to imply it prints automatically now but it does not. Searching the docs for backtrace doesn't give any results.
The text was updated successfully, but these errors were encountered: