Elevate FsWatcherCreate errors to critical

See #251
This commit is contained in:
Félix Saparelli 2022-01-31 03:16:48 +13:00
parent c6f230a168
commit e40744f033
3 changed files with 28 additions and 6 deletions

View File

@ -3,28 +3,33 @@ use std::convert::Infallible;
use clap::ArgMatches;
use miette::{Report, Result};
use tracing::error;
use watchexec::{config::InitConfig, error::RuntimeError, handler::SyncFnHandler};
use watchexec::{config::InitConfig, error::RuntimeError, handler::SyncFnHandler, ErrorHook};
pub fn init(_args: &ArgMatches<'static>) -> Result<InitConfig> {
let mut config = InitConfig::default();
config.on_error(SyncFnHandler::from(
|data| -> std::result::Result<(), Infallible> {
|err: ErrorHook| -> std::result::Result<(), Infallible> {
if let RuntimeError::IoError {
about: "waiting on process group",
..
} = data
} = err.error
{
// "No child processes" and such
// these are often spurious, so condemn them to -v only
error!("{}", data);
error!("{}", err.error);
return Ok(());
}
if let RuntimeError::FsWatcherCreate { .. } = err.error {
err.elevate();
return Ok(());
}
if cfg!(debug_assertions) {
eprintln!("[[{:?}]]", data);
eprintln!("[[{:?}]]", err.error);
}
eprintln!("[[Error (not fatal)]]\n{}", Report::new(data));
eprintln!("[[Error (not fatal)]]\n{}", Report::new(err.error));
Ok(())
},

View File

@ -24,6 +24,15 @@ pub enum CriticalError {
#[diagnostic(code(watchexec::critical::external))]
External(#[from] Box<dyn std::error::Error + Send + Sync>),
/// For elevated runtime errors.
///
/// This should be used for runtime errors elevated to critical. This currently does not happen
/// in watchexec, but it is possible in the future. This variant is useful with the `on_error`
/// runtime error handler; see [`ErrorHook`](crate::ErrorHook).
#[error("elevated: {0}")]
#[diagnostic(code(watchexec::critical::elevated_runtime))]
Elevated(#[source] RuntimeError),
/// A critical I/O error occurred.
#[error("io({about}): {err}")]
#[diagnostic(code(watchexec::critical::io_error))]

View File

@ -250,4 +250,12 @@ impl ErrorHook {
pub fn critical(self, critical: CriticalError) {
self.critical.set(critical).ok();
}
/// Elevate the current runtime error to critical.
///
/// This is a shorthand method for `ErrorHook::critical(CriticalError::Elevated(error))`.
pub fn elevate(self) {
let Self { error, critical } = self;
critical.set(CriticalError::Elevated(error)).ok();
}
}