Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Add backtrace to error output when RUST_BACKTRACE=1 #311

Merged
8 commits merged into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rust-version = "1.60.0"
maintenance = {status = "actively-developed"}

[dependencies]
anyhow = "1.0.51"
anyhow = {version = "1.0.51", features = ["backtrace"]}
async-nats = "0.18.0"
atelier_core = "0.2"
bytes = "1.0"
Expand Down
29 changes: 16 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,32 @@ async fn main() {
0
}
Err(e) => {
let trace = e
.chain()
.skip(1)
.map(|e| format!("{}", e))
.collect::<Vec<String>>();

match output_kind {
OutputKind::Json => {
let mut map = HashMap::new();
map.insert("success".to_string(), json!(false));
map.insert("error".to_string(), json!(e.to_string()));
if !trace.is_empty() {
map.insert("trace".to_string(), json!(trace));

let error_chain = e
.chain()
.skip(1)
.map(|e| format!("{}", e))
.collect::<Vec<String>>();

if !error_chain.is_empty() {
map.insert("error_chain".to_string(), json!(error_chain));
}

let backtrace = e.backtrace().to_string();

if !backtrace.is_empty() && backtrace != "disabled backtrace" {
map.insert("backtrace".to_string(), json!(backtrace));
}

eprintln!("\n{}", serde_json::to_string_pretty(&map).unwrap());
}
OutputKind::Text => {
eprintln!("\n{}", e);
if !trace.is_empty() {
eprintln!("Error trace:");
eprintln!("{}", trace.join("\n"));
}
eprintln!("\n{:?}", e);
}
}
1
Expand Down