From 17106be497bc84f56e543e5a2cb0f634cb7d792b Mon Sep 17 00:00:00 2001 From: eir Date: Thu, 26 Apr 2018 14:14:37 -0700 Subject: [PATCH] Send Appropriate Response on Error Users can now place custom error pages in static/error/.html e.g. static/error/404.html If no custom page is found, a default plaintext describing the error is served. --- src/cmd/serve.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 1ae7a01..85cbe09 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -22,7 +22,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use std::env; -use std::fs::remove_dir_all; +use std::fs::{File, remove_dir_all}; use std::io; use std::path::{Path, PathBuf}; use std::sync::mpsc::channel; @@ -58,6 +58,31 @@ enum ChangeKind { // errors const LIVE_RELOAD: &'static str = include_str!("livereload.js"); +struct ErrCatcher; + +impl AfterMiddleware for ErrCatcher { + + fn catch(&self, _: &mut Request, err: IronError) -> IronResult { + + let err_status = err.response.status.unwrap_or(status::InternalServerError); + + // Search for e.g. "static/error/404.html" + let err_page = File::open( format!("static/error/{}.html", err_status.to_u16()) ); + + if err_page.is_ok() { + let mut err_res = Response::with(( err_status, err_page.unwrap() )); + err_res.headers.set(ContentType::html()); + + return Ok(err_res); + } + + // No custom error page, serve default + let mut err_res = Response::with(( err_status, format!("Error: {}.", err_status) )); + err_res.headers.set(ContentType::plaintext()); + + Ok(err_res) + } +} fn livereload_handler(_: HttpRequest) -> &'static str { LIVE_RELOAD