1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::convert::Infallible;

use miette::Report;
use tracing::error;
use watchexec::{
	config::InitConfig,
	error::{FsWatcherError, RuntimeError},
	handler::SyncFnHandler,
	ErrorHook,
};

use crate::args::Args;

pub fn init(_args: &Args) -> InitConfig {
	let mut config = InitConfig::default();
	config.on_error(SyncFnHandler::from(
		|err: ErrorHook| -> std::result::Result<(), Infallible> {
			if let RuntimeError::IoError {
				about: "waiting on process group",
				..
			} = err.error
			{
				// "No child processes" and such
				// these are often spurious, so condemn them to -v only
				error!("{}", err.error);
				return Ok(());
			}

			if let RuntimeError::FsWatcher {
				err:
					FsWatcherError::Create { .. }
					| FsWatcherError::TooManyWatches { .. }
					| FsWatcherError::TooManyHandles { .. },
				..
			} = err.error
			{
				err.elevate();
				return Ok(());
			}

			if cfg!(debug_assertions) {
				eprintln!("[[{:?}]]", err.error);
			}

			eprintln!("[[Error (not fatal)]]\n{}", Report::new(err.error));

			Ok(())
		},
	));

	config
}