diff options
author | boastful-squirrel <boastful.squirrel@gmail.com> | 2019-05-01 15:01:41 +0000 |
---|---|---|
committer | boastful-squirrel <boastful.squirrel@gmail.com> | 2019-05-01 15:01:41 +0000 |
commit | e4bb38b17db665a8167feab9347494445385ebb3 (patch) | |
tree | c4608fd9ea443cf69113371c344bcf842b36b310 /src | |
parent | Display HTTP authentication errors (diff) | |
download | miniserve-e4bb38b17db665a8167feab9347494445385ebb3.tar.gz miniserve-e4bb38b17db665a8167feab9347494445385ebb3.zip |
Display 404 error
Diffstat (limited to '')
-rw-r--r-- | src/errors.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 25 |
2 files changed, 27 insertions, 2 deletions
diff --git a/src/errors.rs b/src/errors.rs index a22127e..e5d2e41 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -72,6 +72,10 @@ pub enum ContextualError { /// This error might occur when an HTTP request is invalid #[fail(display = "Invalid HTTP request\ncaused by: {}", _0)] InvalidHTTPRequestError(String), + + /// This error might occur when trying to access a page that does not exist + #[fail(display = "Route {} could not be found", _0)] + RouteNotFoundError(String), } pub fn log_error_chain(description: String) { diff --git a/src/main.rs b/src/main.rs index c5c81f2..4b2118c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ #![feature(proc_macro_hygiene)] use actix_web::http::Method; -use actix_web::{fs, middleware, server, App}; +use actix_web::{fs, middleware, server, App, HttpRequest, HttpResponse}; use clap::crate_version; use simplelog::{Config, LevelFilter, TermLogger}; use std::io::{self, Write}; @@ -247,7 +247,8 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { default_color_scheme, u_r.clone(), ) - }), + }) + .default_handler(p404), ) } }; @@ -274,3 +275,23 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { app.resource(&full_route, |r| r.f(listing::file_handler)) } } + +fn p404(req: &HttpRequest<crate::MiniserveConfig>) -> Result<HttpResponse, io::Error> { + let err_404 = ContextualError::RouteNotFoundError(req.uri().to_string()); + let default_color_scheme = req.state().default_color_scheme; + + errors::log_error_chain(err_404.to_string()); + + Ok(actix_web::HttpResponse::NotFound().body( + renderer::render_error( + &err_404.to_string(), + "/", + None, + None, + default_color_scheme, + default_color_scheme, + false, + ) + .into_string(), + )) +} |