Better error if address in use

Fix #72
This commit is contained in:
Vincent Prouillet 2017-05-21 00:00:41 +09:00
parent 33faf6fe70
commit 2a11b9d116
3 changed files with 15 additions and 18 deletions

View file

@ -53,8 +53,6 @@ fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &st
} }
} }
// Most of it taken from mdbook
pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> { pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
let start = Instant::now(); let start = Instant::now();
let mut site = Site::new(env::current_dir().unwrap(), config_file)?; let mut site = Site::new(env::current_dir().unwrap(), config_file)?;
@ -92,7 +90,8 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
mount.mount("/livereload.js", livereload_handler); mount.mount("/livereload.js", livereload_handler);
// Starts with a _ to not trigger the unused lint // Starts with a _ to not trigger the unused lint
// we need to assign to a variable otherwise it will block // we need to assign to a variable otherwise it will block
let _iron = Iron::new(mount).http(address.as_str()).unwrap(); let _iron = Iron::new(mount).http(address.as_str())
.chain_err(|| "Can't start the webserver")?;
// The websocket for livereload // The websocket for livereload
let ws_server = WebSocket::new(|output: Sender| { let ws_server = WebSocket::new(|output: Sender| {
@ -123,8 +122,6 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
use notify::DebouncedEvent::*; use notify::DebouncedEvent::*;
loop { loop {
// See https://github.com/spf13/hugo/blob/master/commands/hugo.go
// for a more complete version of that
match rx.recv() { match rx.recv() {
Ok(event) => { Ok(event) => {
match event { match event {
@ -166,7 +163,6 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
} }
} }
/// Returns whether the path we received corresponds to a temp file created /// Returns whether the path we received corresponds to a temp file created
/// by an editor or the OS /// by an editor or the OS
fn is_temp_file(path: &Path) -> bool { fn is_temp_file(path: &Path) -> bool {
@ -195,7 +191,6 @@ fn is_temp_file(path: &Path) -> bool {
} }
} }
/// Detect what changed from the given path so we have an idea what needs /// Detect what changed from the given path so we have an idea what needs
/// to be reloaded /// to be reloaded
fn detect_change_kind(pwd: &str, path: &Path) -> (ChangeKind, String) { fn detect_change_kind(pwd: &str, path: &Path) -> (ChangeKind, String) {
@ -222,8 +217,8 @@ mod tests {
use super::{is_temp_file, detect_change_kind, ChangeKind}; use super::{is_temp_file, detect_change_kind, ChangeKind};
#[test] #[test]
fn test_can_recognize_temp_files() { fn can_recognize_temp_files() {
let testcases = vec![ let test_cases = vec![
Path::new("hello.swp"), Path::new("hello.swp"),
Path::new("hello.swx"), Path::new("hello.swx"),
Path::new(".DS_STORE"), Path::new(".DS_STORE"),
@ -235,14 +230,14 @@ mod tests {
Path::new("#hello.html"), Path::new("#hello.html"),
]; ];
for t in testcases { for t in test_cases {
assert!(is_temp_file(&t)); assert!(is_temp_file(&t));
} }
} }
#[test] #[test]
fn test_can_detect_kind_of_changes() { fn can_detect_kind_of_changes() {
let testcases = vec![ let test_cases = vec![
( (
(ChangeKind::Templates, "/templates/hello.html".to_string()), (ChangeKind::Templates, "/templates/hello.html".to_string()),
"/home/vincent/site", Path::new("/home/vincent/site/templates/hello.html") "/home/vincent/site", Path::new("/home/vincent/site/templates/hello.html")
@ -257,7 +252,7 @@ mod tests {
), ),
]; ];
for (expected, pwd, path) in testcases { for (expected, pwd, path) in test_cases {
assert_eq!(expected, detect_change_kind(&pwd, &path)); assert_eq!(expected, detect_change_kind(&pwd, &path));
} }
} }

View file

@ -66,9 +66,11 @@ pub fn report_elapsed_time(instant: Instant) {
/// Display an error message and the actual error(s) /// Display an error message and the actual error(s)
pub fn unravel_errors(message: &str, error: &Error) { pub fn unravel_errors(message: &str, error: &Error) {
if message.len() > 0 {
self::error(message); self::error(message);
self::error(&format!("Error: {}", error)); }
for e in error.iter().skip(1) { self::error(&format!("Error: {}", error));
self::error(&format!("Reason: {}", e)); for e in error.iter().skip(1) {
} self::error(&format!("Reason: {}", e));
}
} }

View file

@ -70,7 +70,7 @@ fn main() {
match cmd::serve(interface, port, config_file) { match cmd::serve(interface, port, config_file) {
Ok(()) => (), Ok(()) => (),
Err(e) => { Err(e) => {
console::unravel_errors("Failed to build the site", &e); console::unravel_errors("", &e);
::std::process::exit(1); ::std::process::exit(1);
}, },
}; };