Skip to content

Commit

Permalink
feat(panic): improved backtrace handling
Browse files Browse the repository at this point in the history
Fixes: #92
  • Loading branch information
zkat committed Mar 31, 2023
1 parent 0b445dc commit d24e60d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
16 changes: 3 additions & 13 deletions Cargo.toml
Expand Up @@ -26,6 +26,7 @@ supports-color = { version = "2.0.0", optional = true }
supports-unicode = { version = "2.0.0", optional = true }
backtrace = { version = "0.3.61", optional = true }
terminal_size = { version = "0.1.17", optional = true }
backtrace-ext = { version = "0.1.0", optional = true }

[dev-dependencies]
semver = "1.0.4"
Expand All @@ -41,19 +42,8 @@ lazy_static = "1.4"

[features]
default = []
fancy-no-backtrace = [
"owo-colors",
"is-terminal",
"textwrap",
"terminal_size",
"supports-hyperlinks",
"supports-color",
"supports-unicode",
]
fancy = [
"fancy-no-backtrace",
"backtrace",
]
fancy-no-backtrace = ["owo-colors", "is-terminal", "textwrap", "terminal_size", "supports-hyperlinks", "supports-color", "supports-unicode"]
fancy = ["fancy-no-backtrace", "backtrace-ext"]

[workspace]
members = ["miette-derive"]
Expand Down
26 changes: 12 additions & 14 deletions src/panic.rs
@@ -1,7 +1,3 @@
#![cfg(feature = "fancy")]
use std::fmt::Write;

use backtrace::Backtrace;
use thiserror::Error;

use crate::{self as miette, Context, Diagnostic, Result};
Expand Down Expand Up @@ -29,20 +25,22 @@ pub fn set_panic_hook() {
}

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

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

Expand All @@ -52,10 +50,10 @@ impl Panic {
continue;
}

for (idx, symbol) in symbols.iter().enumerate() {
//Print symbols from this address,
//if there are several addresses
//we need to put it on next line
for (idx, symbol) in symbols[sub_frames].iter().enumerate() {
// Print symbols from this address,
// if there are several addresses
// we need to put it on next line
if idx != 0 {
let _ = write!(backtrace, "\n{:1$}", "", NEXT_SYMBOL_PADDING);
}
Expand All @@ -66,7 +64,7 @@ impl Panic {
let _ = write!(backtrace, " - <unknown>");
}

//See if there is debug information with file name and line
// See if there is debug information with file name and line
if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) {
let _ = write!(
backtrace,
Expand Down

0 comments on commit d24e60d

Please sign in to comment.