From f832e10facae06a60ce05be482138db66e7cbae7 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 21 Apr 2019 17:57:57 +0200 Subject: [PATCH] Add custom error pages --- src/error.rs | 11 ++++++++--- src/main.rs | 19 +++++++++---------- static/404.html | 46 ++++++++++++++++++++++++++++++++++++++++++++++ static/500.html | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 static/404.html create mode 100644 static/500.html diff --git a/src/error.rs b/src/error.rs index 3f8d533..794abf9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,6 @@ +use crate::P500; use actix_web::{HttpResponse, ResponseError}; +use std::fmt; #[derive(Debug)] pub(crate) enum Error { @@ -9,8 +11,9 @@ pub(crate) enum Error { Serial(serde_json::Error), } -impl std::fmt::Display for Error { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { +impl fmt::Display for Error { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + // write!(fmt, "{}", P500) match self { Error::Badge(s) => write!(fmt, "Badge({})", s), Error::Git(e) => write!(fmt, "Git({})", e), @@ -23,7 +26,9 @@ impl std::fmt::Display for Error { impl ResponseError for Error { fn error_response(&self) -> HttpResponse { - HttpResponse::InternalServerError().finish() + HttpResponse::InternalServerError() + .content_type("text/html") + .body(P500) } } diff --git a/src/main.rs b/src/main.rs index 04d217d..38b0b70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,8 @@ struct State { } const INDEX: &str = include_str!("../static/index.html"); +const P404: &str = include_str!("../static/404.html"); +const P500: &str = include_str!("../static/500.html"); const CSS: &str = include_str!("../static/tacit-css.min.css"); #[derive(StructOpt, Debug)] @@ -194,22 +196,18 @@ fn overview(_: web::Path<(String, String)>) -> HttpResponse { #[get("/")] fn index() -> HttpResponse { - let (tx, rx_body) = mpsc::unbounded(); - let _ = tx.unbounded_send(Bytes::from(INDEX.as_bytes())); + HttpResponse::Ok().content_type("text/html").body(INDEX) +} - HttpResponse::Ok() +fn p404() -> HttpResponse { + HttpResponse::NotFound() .content_type("text/html") - .streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))) + .body(P404) } #[get("/tacit-css.min.css")] fn css() -> HttpResponse { - let (tx, rx_body) = mpsc::unbounded(); - let _ = tx.unbounded_send(Bytes::from(CSS.as_bytes())); - - HttpResponse::Ok() - .content_type("text/css") - .streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))) + HttpResponse::Ok().content_type("text/css").body(CSS) } fn main() -> std::io::Result<()> { @@ -234,6 +232,7 @@ fn main() -> std::io::Result<()> { .service(web::resource("/view/github/{user}/{repo}").to(overview)) .service(web::resource("/view/gitlab/{user}/{repo}").to(overview)) .service(web::resource("/view/github/{user}/{repo}").to(overview)) + .default_service(web::resource("").route(web::get().to(p404))) }) .bind(interface)? .run() diff --git a/static/404.html b/static/404.html new file mode 100644 index 0000000..a0b77d9 --- /dev/null +++ b/static/404.html @@ -0,0 +1,46 @@ + + + + + + + + + Page Not Found - Hits-of-Code Badges + + + +
+
+

404 - Page Not Found

+
+ + + + +
+ + + diff --git a/static/500.html b/static/500.html new file mode 100644 index 0000000..860d998 --- /dev/null +++ b/static/500.html @@ -0,0 +1,46 @@ + + + + + + + + + Internal Server Error - Hits-of-Code Badges + + + +
+
+

500 - Internal Server Error

+
+ + + + +
+ + +