Skip to content

Commit b045321

Browse files
committedApr 1, 2023
feat(panic): improved backtrace handling
Fixes: #92
1 parent 0b445dc commit b045321

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed
 

‎Cargo.toml

+3-13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ supports-color = { version = "2.0.0", optional = true }
2626
supports-unicode = { version = "2.0.0", optional = true }
2727
backtrace = { version = "0.3.61", optional = true }
2828
terminal_size = { version = "0.1.17", optional = true }
29+
backtrace-ext = { version = "0.2.1", optional = true }
2930

3031
[dev-dependencies]
3132
semver = "1.0.4"
@@ -41,19 +42,8 @@ lazy_static = "1.4"
4142

4243
[features]
4344
default = []
44-
fancy-no-backtrace = [
45-
"owo-colors",
46-
"is-terminal",
47-
"textwrap",
48-
"terminal_size",
49-
"supports-hyperlinks",
50-
"supports-color",
51-
"supports-unicode",
52-
]
53-
fancy = [
54-
"fancy-no-backtrace",
55-
"backtrace",
56-
]
45+
fancy-no-backtrace = ["owo-colors", "is-terminal", "textwrap", "terminal_size", "supports-hyperlinks", "supports-color", "supports-unicode"]
46+
fancy = ["fancy-no-backtrace", "backtrace", "backtrace-ext"]
5747

5848
[workspace]
5949
members = ["miette-derive"]

‎src/panic.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#![cfg(feature = "fancy")]
2-
use std::fmt::Write;
3-
41
use backtrace::Backtrace;
52
use thiserror::Error;
63

@@ -29,20 +26,22 @@ pub fn set_panic_hook() {
2926
}
3027

3128
#[derive(Debug, Error, Diagnostic)]
32-
#[error("{0}{}", self.maybe_collect_backtrace())]
29+
#[error("{0}{}", Panic::backtrace())]
3330
#[diagnostic(help("set the `RUST_BACKTRACE=1` environment variable to display a backtrace."))]
3431
struct Panic(String);
3532

3633
impl Panic {
37-
fn maybe_collect_backtrace(&self) -> String {
34+
fn backtrace() -> String {
35+
use std::fmt::Write;
3836
if let Ok(var) = std::env::var("RUST_BACKTRACE") {
3937
if !var.is_empty() && var != "0" {
40-
// This is all taken from human-panic: https://github.com/rust-cli/human-panic/blob/master/src/report.rs#L55-L107
4138
const HEX_WIDTH: usize = std::mem::size_of::<usize>() + 2;
42-
//Padding for next lines after frame's address
39+
// Padding for next lines after frame's address
4340
const NEXT_SYMBOL_PADDING: usize = HEX_WIDTH + 6;
4441
let mut backtrace = String::new();
45-
for (idx, frame) in Backtrace::new().frames().iter().skip(26).enumerate() {
42+
let trace = Backtrace::new();
43+
let frames = backtrace_ext::short_frames_strict(&trace).enumerate();
44+
for (idx, (frame, sub_frames)) in frames {
4645
let ip = frame.ip();
4746
let _ = write!(backtrace, "\n{:4}: {:2$?}", idx, ip, HEX_WIDTH);
4847

@@ -52,10 +51,10 @@ impl Panic {
5251
continue;
5352
}
5453

55-
for (idx, symbol) in symbols.iter().enumerate() {
56-
//Print symbols from this address,
57-
//if there are several addresses
58-
//we need to put it on next line
54+
for (idx, symbol) in symbols[sub_frames].iter().enumerate() {
55+
// Print symbols from this address,
56+
// if there are several addresses
57+
// we need to put it on next line
5958
if idx != 0 {
6059
let _ = write!(backtrace, "\n{:1$}", "", NEXT_SYMBOL_PADDING);
6160
}
@@ -66,7 +65,7 @@ impl Panic {
6665
let _ = write!(backtrace, " - <unknown>");
6766
}
6867

69-
//See if there is debug information with file name and line
68+
// See if there is debug information with file name and line
7069
if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) {
7170
let _ = write!(
7271
backtrace,

0 commit comments

Comments
 (0)
Please sign in to comment.