Skip to content

Commit

Permalink
fix: Resolve panic from api search / improve panic handling (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick42d authored Aug 30, 2024
1 parent aa5e407 commit f9c64ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions youtui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ mod taskmanager;
mod ui;
mod view;

// We need this thread_local to ensure we know which is the main thread. Panic
// hook that destructs terminal should only run on the main thread.
thread_local! {
static IS_MAIN_THREAD: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

const CALLBACK_CHANNEL_SIZE: usize = 64;
const EVENT_CHANNEL_SIZE: usize = 256;
const LOG_FILE_NAME: &str = "debug.log";
Expand Down Expand Up @@ -93,10 +99,13 @@ impl Youtui {
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
// Ensure clean return to shell if panic.
IS_MAIN_THREAD.with(|flag| flag.set(true));
std::panic::set_hook(Box::new(|panic_info| {
// If we fail to destruct terminal, ignore the error as panicking anyway.
let _ = destruct_terminal();
println!("{}", panic_info);
if IS_MAIN_THREAD.with(|flag| flag.get()) {
// If we fail to destruct terminal, ignore the error as panicking anyway.
let _ = destruct_terminal();
println!("{}", panic_info);
}
}));
// Setup components
let (callback_tx, callback_rx) = mpsc::channel(CALLBACK_CHANNEL_SIZE);
Expand Down
2 changes: 1 addition & 1 deletion ytmapi-rs/src/parse/artist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<'a> ParseFrom<GetArtistQuery<'a>> for ArtistParams {
};
top_releases.albums = Some(albums);
}
ArtistTopReleaseCategory::Playlists => todo!(),
ArtistTopReleaseCategory::Playlists => (),
ArtistTopReleaseCategory::None => (),
}
}
Expand Down

0 comments on commit f9c64ee

Please sign in to comment.