From 366e5c426f479f6768fa92f47696d3101f2f0c4f Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 22 Sep 2020 12:22:26 +0200 Subject: [PATCH] Avoid panics when binding addresses --- CHANGELOG.md | 2 +- src/cmd/serve.rs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16bf03bd..e167ee17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 0.12.1 (unreleased) -- Add line +- Add line highlighting in code blocks - Fix the new `zola serve` being broken on Windows - Fix slugified taxonomies not being rendered at the right path diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index de3e195c..58ea99b5 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -182,6 +182,12 @@ fn create_new_site( let base_address = format!("{}:{}", base_url, interface_port); let address = format!("{}:{}", interface, interface_port); + + // Stop right there if we can't bind to the address + if Server::try_bind(&address.parse().unwrap()).is_err() { + return Err(format!("Cannot start server on address {}.", address))?; + } + let base_url = if site.config.base_url.ends_with('/') { format!("http://{}/", base_address) } else { @@ -331,7 +337,7 @@ pub fn serve( let ws_server = ws_server .bind(&*ws_address) - .map_err(|_| format!("Address {} is already in use.", &ws_address))?; + .map_err(|_| format!("Cannot bind to address {} for the websocket server. Maybe the port is already in use?", &ws_address))?; thread::spawn(move || { ws_server.run().unwrap();