Revert old listener panic behaviour
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 1m3s
Cargo CI / Lint (pull_request) Successful in 2m36s

This commit is contained in:
Wojciech Kozlowski 2024-03-05 21:28:31 +01:00
parent 88d1dc2b01
commit 930cb5c0ec
2 changed files with 13 additions and 23 deletions

View File

@ -75,12 +75,7 @@ fn with<LIB: ILibrary, DB: IDatabase>(builder: MusicHoardBuilder<LIB, DB>) {
let ui = Ui;
// Run the TUI application.
let result = Tui::run(terminal, app, ui, handler, listener);
if let Err(tui::Error::ListenerPanic(err)) = result {
std::panic::resume_unwind(err);
} else {
result.expect("failed to run tui")
};
Tui::run(terminal, app, ui, handler, listener).expect("failed to run tui");
}
fn with_database<LIB: ILibrary>(db_opt: DbOpt, builder: MusicHoardBuilder<LIB, NoDatabase>) {

View File

@ -16,10 +16,7 @@ use crossterm::{
terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::Backend, Terminal};
use std::{
marker::PhantomData,
{any::Any, io},
};
use std::{io, marker::PhantomData};
use crate::tui::{
app::{IAppAccess, IAppInteract},
@ -29,11 +26,11 @@ use crate::tui::{
ui::IUi,
};
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
Io(String),
Event(String),
ListenerPanic(Box<dyn Any + Send>),
ListenerPanic,
}
impl From<io::Error> for Error {
@ -115,10 +112,9 @@ impl<B: Backend, UI: IUi, APP: IAppInteract + IAppAccess> Tui<B, UI, APP> {
match listener_handle.join() {
Ok(err) => return Err(err.into()),
// Calling std::panic::resume_unwind(err) as recommended by the Rust docs
// will not produce an error message. The panic error message is printed at
// the location of the panic which at the time is hidden by the TUI.
// Therefore, propagate the error for the caller to resume unwinding.
Err(panic) => return Err(Error::ListenerPanic(panic)),
// will not produce an error message. This may be due to the panic simply
// causing the process to abort in which case there is nothing to unwind.
Err(_) => return Err(Error::ListenerPanic),
}
}
@ -256,8 +252,8 @@ mod tests {
let result = Tui::main(terminal, app, ui, handler, listener);
assert!(result.is_err());
let recv_err = EventError::Recv.to_string();
matches!(result.unwrap_err(), Error::Event(err) if err == recv_err);
let error = EventError::Recv;
assert_eq!(result.unwrap_err(), Error::Event(error.to_string()));
}
#[test]
@ -281,9 +277,8 @@ mod tests {
let result = Tui::main(terminal, app, ui, handler, listener);
assert!(result.is_err());
let io_err =
EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error")).to_string();
matches!(result.unwrap_err(), Error::Event(err) if err == io_err);
let error = EventError::Io(io::Error::new(io::ErrorKind::Interrupted, "error"));
assert_eq!(result.unwrap_err(), Error::Event(error.to_string()));
}
#[test]
@ -305,14 +300,14 @@ mod tests {
let result = Tui::main(terminal, app, ui, handler, listener);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), Error::ListenerPanic(_)));
assert_eq!(result.unwrap_err(), Error::ListenerPanic);
}
#[test]
fn errors() {
let io_err: Error = io::Error::new(io::ErrorKind::Interrupted, "error").into();
let event_err: Error = EventError::Recv.into();
let listener_err = Error::ListenerPanic(Box::new("hello"));
let listener_err = Error::ListenerPanic;
assert!(!format!("{:?}", io_err).is_empty());
assert!(!format!("{:?}", event_err).is_empty());