Add docs for error_handler as it's a bit tricky

This commit is contained in:
Félix Saparelli 2021-08-21 21:12:40 +12:00
parent 816313303a
commit 019018c93e
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
5 changed files with 47 additions and 6 deletions

View File

@ -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<dyn Handler<RuntimeError> + Send>,

View File

@ -162,7 +162,7 @@ where
impl<T> Handler<T> for () {
fn handle(&mut self, _data: T) -> Result<(), Box<dyn Error>> {
Ok(()).map_err(|e: std::convert::Infallible| Box::new(e) as _)
Ok::<(), std::convert::Infallible>(()).map_err(|e| Box::new(e) as _)
}
}

View File

@ -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)]

View File

@ -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 {

View File

@ -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(())
}