Refactor StdBackend error handling for file watcher reliability

- Updated the `new` method in `StdBackend` to improve error handling for file watching.
- Introduced specific handling for `WatcherCriticalError::RescanRequired`, allowing the watcher to recover from rapid changes while logging a warning.
- Ensured that all other critical errors will terminate the process, maintaining reliability in file watching operations.
This commit is contained in:
Joe 2026-02-06 19:51:46 -06:00
parent fa5393121e
commit a2c8a583a6

View file

@ -64,11 +64,29 @@ pub struct StdBackend {
} }
impl StdBackend { impl StdBackend {
/// Creates a new StdBackend with default error handling (logs and exits on critical errors). /// Creates a new StdBackend with default error handling.
///
/// `RescanRequired` is treated as recoverable (the debouncer will re-walk
/// the directory tree). All other critical errors terminate the process
/// because file watching cannot continue.
pub fn new() -> StdBackend { pub fn new() -> StdBackend {
Self::new_with_error_handler(Box::new(|err| { Self::new_with_error_handler(Box::new(|err| {
log::error!("{}. File watching is no longer reliable.", err); match &err {
std::process::exit(1); WatcherCriticalError::RescanRequired => {
// Recoverable: the debouncer lost some events due to rapid
// changes and will rescan the watched directories. Log a
// warning but keep the watcher thread alive.
log::warn!(
"File watcher requested rescan due to rapid changes. \
Some file events may have been missed."
);
false // keep watcher running
}
_ => {
log::error!("{}. File watching is no longer reliable.", err);
std::process::exit(1);
}
}
})) }))
} }