From 8be3cb4a888cb9a7c146b7102366478c157e36e6 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 5 Jul 2019 14:07:36 +0200 Subject: [PATCH] Don't panic on error in watch --- src/lib.rs | 27 ++++++++++++++++++++++++--- src/server.rs | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6ea542a..7cf6430 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 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: 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()); diff --git a/src/server.rs b/src/server.rs index ea27336..9331bdd 100644 --- a/src/server.rs +++ b/src/server.rs @@ -22,7 +22,7 @@ fn main() { if args.len() == 3 && args[1] == "-w" { if let Err(e) = watch(&args[2]) { - error!("{:?}", e); + error!("{}", e); } }