Don't panic on error in watch

This commit is contained in:
Thomas Forgione 2019-07-05 14:07:36 +02:00
parent 89805cd351
commit 8be3cb4a88
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
2 changed files with 25 additions and 4 deletions

View File

@ -1,7 +1,7 @@
#[macro_use]
extern crate log;
use std::{io, thread};
use std::{io, thread, fmt};
use std::env::current_dir;
use std::process::{Command, ExitStatus};
use std::path::{Path, PathBuf};
@ -34,6 +34,16 @@ impl From<io::Error> for Error {
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::NoBuilderFound => write!(fmt, "no builder found"),
Error::IoError(e) => write!(fmt, "i/o error occured: {}", e),
Error::CommandError(command, _, status) => write!(fmt, "command {} exited with status code {}", command, status),
}
}
}
/// Tests if a file is in a directory.
pub fn contains_file(path: &str, file: &str) -> bool {
let mut path = PathBuf::from(path);
@ -118,8 +128,19 @@ pub fn watch<P: AsRef<Path>>(p: P) -> Result<(), Error> {
thread::spawn(move || {
let mut builder = GeneralBuilder::new();
let (tx, rx) = channel();
let mut watcher = watcher(tx, Duration::from_millis(200)).unwrap();
watcher.watch(&path, RecursiveMode::Recursive).unwrap();
let mut watcher = match watcher(tx, Duration::from_millis(200)) {
Ok(w) => w,
Err(e) => {
warn!("couldn't watch directory {}: {}", path.display(), e);
return;
},
};
if let Err(e) = watcher.watch(&path, RecursiveMode::Recursive) {
warn!("couldn't watch directory {}: {}", path.display(), e);
return;
}
info!("watching {}", path.display());

View File

@ -22,7 +22,7 @@ fn main() {
if args.len() == 3 && args[1] == "-w" {
if let Err(e) = watch(&args[2]) {
error!("{:?}", e);
error!("{}", e);
}
}