diff options
| author | Kunal Mohan <44079328+kunalmohan@users.noreply.github.com> | 2020-12-03 20:05:16 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-03 15:35:16 +0100 |
| commit | 56c53d24870d7470a6aa336788093860ea05c9e7 (patch) | |
| tree | 68196a52a7fb1d92b106d72643b92ffb2d9d0733 /src/errors.rs | |
| parent | fix(threads): a race condition, build-times, and warnings (#79) (diff) | |
| download | sl0ppij-56c53d24870d7470a6aa336788093860ea05c9e7.tar.gz sl0ppij-56c53d24870d7470a6aa336788093860ea05c9e7.tar.xz sl0ppij-56c53d24870d7470a6aa336788093860ea05c9e7.zip | |
feat(infra): add custom panic hook. Print backtrace and thread, error info on panic. (#75)
* Add custom panic hook. Print backtrace and thread, error info on panic.
* use sync_channel and SyncSender
* nit fixes and cleanup
* disable custom panic hook while running tests
* make separate errors.rs file and address other review comments
* improve panic message
* debug: does increasing time between snapshots make tests pass? (this is temporary)
* fix(tests): suspend before sending quit command
* fix(tests): add missing use
* style(format): commas are important apparently
* fix(tests): can we get away with reducing the QUIT suspense time?
* fix(tests): can we get away with 50?
Co-authored-by: Aram Drevekenin <aram@poor.dev>
Diffstat (limited to 'src/errors.rs')
| -rw-r--r-- | src/errors.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 00000000..26e0010d --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,56 @@ +use crate::AppInstruction; +use backtrace::Backtrace; +use std::panic::PanicInfo; +use std::sync::mpsc::SyncSender; +use std::{process, thread}; + +pub fn handle_panic(info: &PanicInfo<'_>, send_app_instructions: &SyncSender<AppInstruction>) { + let backtrace = Backtrace::new(); + let thread = thread::current(); + let thread = thread.name().unwrap_or("unnamed"); + + let msg = match info.payload().downcast_ref::<&'static str>() { + Some(s) => Some(*s), + None => info.payload().downcast_ref::<String>().map(|s| &**s), + }; + + let backtrace = match (info.location(), msg) { + (Some(location), Some(msg)) => { + format!( + "\nthread '{}' panicked at '{}': {}:{}\n{:?}", + thread, + msg, + location.file(), + location.line(), + backtrace + ) + } + (Some(location), None) => { + format!( + "\nthread '{}' panicked: {}:{}\n{:?}", + thread, + location.file(), + location.line(), + backtrace + ) + } + (None, Some(msg)) => { + format!( + "\nthread '{}' panicked at '{}'\n{:?}", + thread, msg, backtrace + ) + } + (None, None) => { + format!("\nthread '{}' panicked\n{:?}", thread, backtrace) + } + }; + + if thread == "main" { + println!("{}", backtrace); + process::exit(1); + } else { + send_app_instructions + .send(AppInstruction::Error(backtrace)) + .unwrap(); + } +} |
