Skip to content

Commit

Permalink
feat(panic): add backtrace support to panic handler and move set_pani…
Browse files Browse the repository at this point in the history
…c_hook into fancy features
  • Loading branch information
zkat committed Sep 21, 2021
1 parent 11a708a commit 183ecb9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ supports-hyperlinks = { version = "1.1.0", optional = true }
supports-color = { version = "1.0.2", optional = true }
supports-unicode = { version = "1.0.0", optional = true }
itertools = { version = "0.10.1", optional = true }
backtrace = { version = "0.3.61", optional = true }

[dev-dependencies]
semver = "1.0.4"
Expand All @@ -49,7 +50,8 @@ fancy = [
"supports-hyperlinks",
"supports-color",
"supports-unicode",
"itertools"
"itertools",
"backtrace"
]

[workspace]
Expand Down
61 changes: 59 additions & 2 deletions src/panic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#![cfg(feature = "fancy")]
use std::fmt::Write;

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

use crate::{self as miette, Context, Diagnostic, Result};

/// Tells miette to render panics using its rendering engine.
pub fn set_panic_hook() {
std::panic::set_hook(Box::new(|info| {
std::panic::set_hook(Box::new(move |info| {
let mut message = "Something went wrong".to_string();
let payload = info.payload();
if let Some(msg) = payload.downcast_ref::<&str>() {
Expand All @@ -25,6 +29,59 @@ pub fn set_panic_hook() {
}

#[derive(Debug, Error, Diagnostic)]
#[error("{0}")]
#[error("{0}{}", self.maybe_collect_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 {
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
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 ip = frame.ip();
let _ = write!(backtrace, "\n{:4}: {:2$?}", idx, ip, HEX_WIDTH);

let symbols = frame.symbols();
if symbols.is_empty() {
let _ = write!(backtrace, " - <unresolved>");
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
if idx != 0 {
let _ = write!(backtrace, "\n{:1$}", "", NEXT_SYMBOL_PADDING);
}

if let Some(name) = symbol.name() {
let _ = write!(backtrace, " - {}", name);
} else {
let _ = write!(backtrace, " - <unknown>");
}

//See if there is debug information with file name and line
if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) {
let _ = write!(
backtrace,
"\n{:3$}at {}:{}",
"",
file.display(),
line,
NEXT_SYMBOL_PADDING
);
}
}
}
return backtrace;
}
}
"".into()
}
}

0 comments on commit 183ecb9

Please sign in to comment.