diff --git a/lib/src/config.rs b/lib/src/config.rs index 7567411..2978ccc 100644 --- a/lib/src/config.rs +++ b/lib/src/config.rs @@ -47,7 +47,17 @@ pub struct InitConfig { /// If the handler errors, [_that_ error][crate::error::RuntimeError::Handler] is immediately /// given to the handler. If that second handler call errors as well, its error is ignored. /// - /// Only used at construction time, cannot be changed via reconfiguration. + /// # Examples + /// + /// ``` + /// # use std::convert::Infallible; + /// # use watchexec::config::InitConfigBuilder; + /// InitConfigBuilder::default() + /// .error_handler(Box::new(|err| async move { + /// tracing::error!("{}", err); + /// Ok::<(), Infallible>(()) + /// })); + /// ``` #[builder(default = "Box::new(()) as _")] pub error_handler: Box + Send>, diff --git a/lib/src/handler.rs b/lib/src/handler.rs index 5e64a7b..7d8ff68 100644 --- a/lib/src/handler.rs +++ b/lib/src/handler.rs @@ -162,7 +162,7 @@ where impl Handler for () { fn handle(&mut self, _data: T) -> Result<(), Box> { - Ok(()).map_err(|e: std::convert::Infallible| Box::new(e) as _) + Ok::<(), std::convert::Infallible>(()).map_err(|e| Box::new(e) as _) } } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 0ff7e7b..4073321 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -16,10 +16,10 @@ //! //! #[tokio::main] //! async fn main() { -//! let init = InitConfigBuilder::new() +//! let init = InitConfigBuilder::default() //! .error_handler(PrintDebug(std::io::stderr())); //! -//! let mut runtime = RuntimeConfigBuilder::new() +//! let mut runtime = RuntimeConfigBuilder::default() //! config.pathset(["watchexec.conf"]); //! //! let conf = YourConfigFormat::load_from_file("watchexec.conf").await?; @@ -62,11 +62,11 @@ pub mod command; pub mod error; pub mod event; pub mod fs; -pub mod handler; pub mod signal; // the core experience pub mod config; +pub mod handler; mod watchexec; #[doc(inline)] diff --git a/lib/src/watchexec.rs b/lib/src/watchexec.rs index 7657cb7..50eb3fc 100644 --- a/lib/src/watchexec.rs +++ b/lib/src/watchexec.rs @@ -70,7 +70,7 @@ impl Watchexec { let error_hook = subtask!(error_hook(er_r, eh)); - try_join!(action, fs, signal).map(drop) + try_join!(action, error_hook, fs, signal).map(drop) }); Ok(Arc::new(Self { diff --git a/lib/tests/error_handler.rs b/lib/tests/error_handler.rs new file mode 100644 index 0000000..88221c9 --- /dev/null +++ b/lib/tests/error_handler.rs @@ -0,0 +1,31 @@ +use std::time::Duration; + +use tokio::time::sleep; +use watchexec::{ + config::{InitConfigBuilder, RuntimeConfigBuilder}, + Watchexec, +}; + +#[tokio::main] +async fn main() -> color_eyre::eyre::Result<()> { + tracing_subscriber::fmt::init(); + color_eyre::install()?; + + let init = InitConfigBuilder::default() + .error_handler(Box::new(|err| async move { + eprintln!("Watchexec Runtime Error: {}", err); + Ok::<(), std::convert::Infallible>(()) + })) + .build()?; + + let runtime = RuntimeConfigBuilder::default().build()?; + + let wx = Watchexec::new(init, runtime)?; + wx.main(); + + // TODO: induce an error here + + sleep(Duration::from_secs(1)).await; + + Ok(()) +}