1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

Merge pull request #829 from Zokrates/panic-hook

Add a custom panic hook
This commit is contained in:
Thibaut Schaeffer 2021-04-22 16:31:52 +02:00 committed by GitHub
commit 50d51df905
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 14 deletions

View file

@ -0,0 +1 @@
Add a custom panic hook to handle internal compiler errors more gracefully

View file

@ -1,3 +1,5 @@
#![feature(panic_info_message)]
#![feature(backtrace)]
//
// @file bin.rs
// @author Jacob Eberhardt <jacob.eberhardt@tu-berlin.de>
@ -15,6 +17,9 @@ use clap::{App, AppSettings, Arg};
use ops::*;
fn main() {
// set a custom panic hook
std::panic::set_hook(Box::new(panic_hook));
cli().unwrap_or_else(|e| {
println!("{}", e);
std::process::exit(1);
@ -49,21 +54,49 @@ fn cli() -> Result<(), String> {
.get_matches();
match matches.subcommand() {
("compile", Some(sub_matches)) => compile::exec(sub_matches)?,
("check", Some(sub_matches)) => check::exec(sub_matches)?,
("compute-witness", Some(sub_matches)) => compute_witness::exec(sub_matches)?,
("compile", Some(sub_matches)) => compile::exec(sub_matches),
("check", Some(sub_matches)) => check::exec(sub_matches),
("compute-witness", Some(sub_matches)) => compute_witness::exec(sub_matches),
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
("setup", Some(sub_matches)) => setup::exec(sub_matches)?,
("export-verifier", Some(sub_matches)) => export_verifier::exec(sub_matches)?,
("setup", Some(sub_matches)) => setup::exec(sub_matches),
("export-verifier", Some(sub_matches)) => export_verifier::exec(sub_matches),
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
("generate-proof", Some(sub_matches)) => generate_proof::exec(sub_matches)?,
("print-proof", Some(sub_matches)) => print_proof::exec(sub_matches)?,
("generate-proof", Some(sub_matches)) => generate_proof::exec(sub_matches),
("print-proof", Some(sub_matches)) => print_proof::exec(sub_matches),
#[cfg(any(feature = "bellman", feature = "ark", feature = "libsnark"))]
("verify", Some(sub_matches)) => verify::exec(sub_matches)?,
("verify", Some(sub_matches)) => verify::exec(sub_matches),
_ => unreachable!(),
};
}
}
Ok(())
fn panic_hook(pi: &std::panic::PanicInfo) {
let location = pi
.location()
.map(|l| format!("({})", l))
.unwrap_or_default();
let message = pi
.message()
.map(|m| format!("{}", m))
.or_else(|| pi.payload().downcast_ref::<&str>().map(|p| p.to_string()));
if let Some(s) = message {
println!("{} {}", s, location);
} else {
println!("The compiler unexpectedly panicked {}", location);
}
#[cfg(debug_assertions)]
{
use std::backtrace::{Backtrace, BacktraceStatus};
let backtrace = Backtrace::capture();
if backtrace.status() == BacktraceStatus::Captured {
println!("rust backtrace:\n{}", backtrace);
}
}
println!("This is unexpected, please submit a full bug report at https://github.com/Zokrates/ZoKrates/issues");
}
#[cfg(test)]

View file

@ -146,10 +146,8 @@ fn cli_generate_proof<T: Field, S: Scheme<T>, B: Backend<T, S>>(
// deserialize witness
let witness_path = Path::new(sub_matches.value_of("witness").unwrap());
let witness_file = match File::open(&witness_path) {
Ok(file) => file,
Err(why) => panic!("Could not open {}: {}", witness_path.display(), why),
};
let witness_file = File::open(&witness_path)
.map_err(|why| format!("Could not open {}: {}", witness_path.display(), why))?;
let witness = ir::Witness::read(witness_file)
.map_err(|why| format!("Could not load witness: {:?}", why))?;