Handle runtime errors properly

This commit is contained in:
Félix Saparelli 2022-07-09 23:22:18 +12:00
parent 1c1fb4d8ad
commit 537182c16e
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
2 changed files with 34 additions and 5 deletions

View File

@ -1,15 +1,44 @@
use std::convert::Infallible;
use miette::Result;
use watchexec::{config::InitConfig, handler::SyncFnHandler};
use miette::{Report, Result};
use tracing::error;
use watchexec::{
config::InitConfig,
error::{FsWatcherError, RuntimeError},
handler::SyncFnHandler,
ErrorHook,
};
use crate::args::Args;
pub fn init(_args: &Args) -> Result<InitConfig> {
let mut config = InitConfig::default();
config.on_error(SyncFnHandler::from(
|data| -> std::result::Result<(), Infallible> {
eprintln!("[[{:?}]]", data);
|err: ErrorHook| -> std::result::Result<(), Infallible> {
if let RuntimeError::IoError {
about: "waiting on process group",
..
} = err.error
{
// "No child processes" and such
// these are often spurious, so condemn them to --debug only
error!("{}", err.error);
return Ok(());
}
if let RuntimeError::FsWatcher {
err:
FsWatcherError::Create { .. }
| FsWatcherError::TooManyWatches { .. }
| FsWatcherError::TooManyHandles { .. },
..
} = err.error
{
err.elevate();
return Ok(());
}
eprintln!("[[Error (not fatal)]]\n{}", Report::new(err.error));
Ok(())
},
));

View File

@ -226,7 +226,7 @@ pub fn runtime(args: &Args, command_order: Vec<&'static str>) -> Result<RuntimeC
if let Some(runs) = quit_after_n.clone() {
if runs.load(Ordering::SeqCst) == 0 {
debug!("quitting after n triggers");
eprintln!("[[--quit after n--]]");
action.outcome(Outcome::Exit);
return fut;
}