Templates for error messages

This commit is contained in:
Magnus Hoff 2017-08-20 22:39:52 +02:00
parent 4a37a91ece
commit d9c3bf1191
3 changed files with 23 additions and 4 deletions

View file

@ -13,11 +13,19 @@ lazy_static! {
#[derive(BartDisplay)] #[derive(BartDisplay)]
#[template = "templates/layout.html"] #[template = "templates/layout.html"]
pub struct Layout<'a, T: 'a + fmt::Display> { struct Layout<'a, T: 'a + fmt::Display> {
pub title: &'a str, pub title: &'a str,
pub body: &'a T, pub body: &'a T,
} }
#[derive(BartDisplay)]
#[template = "templates/404.html"]
struct NotFound;
#[derive(BartDisplay)]
#[template = "templates/500.html"]
struct InternalServerError;
pub struct Site { pub struct Site {
state: State state: State
} }
@ -43,7 +51,10 @@ impl Service for Site {
futures::finished( futures::finished(
Response::new() Response::new()
.with_header(ContentType(TEXT_HTML.clone())) .with_header(ContentType(TEXT_HTML.clone()))
.with_body(format!("Page not found")) .with_body(Layout {
title: "Not found",
body: &NotFound,
}.to_string())
.with_status(hyper::StatusCode::NotFound) .with_status(hyper::StatusCode::NotFound)
).boxed() ).boxed()
} else { } else {
@ -64,7 +75,10 @@ impl Service for Site {
futures::finished( futures::finished(
Response::new() Response::new()
.with_header(ContentType(TEXT_HTML.clone())) .with_header(ContentType(TEXT_HTML.clone()))
.with_body(format!("Article not found.")) .with_body(Layout {
title: "Not found",
body: &NotFound,
}.to_string())
.with_status(hyper::StatusCode::NotFound) .with_status(hyper::StatusCode::NotFound)
).boxed() ).boxed()
}, },
@ -73,7 +87,10 @@ impl Service for Site {
futures::finished( futures::finished(
Response::new() Response::new()
.with_header(ContentType(TEXT_HTML.clone())) .with_header(ContentType(TEXT_HTML.clone()))
.with_body(format!("Internal server error")) .with_body(Layout {
title: "Internal server error",
body: &InternalServerError,
}.to_string())
.with_status(hyper::StatusCode::InternalServerError) .with_status(hyper::StatusCode::InternalServerError)
).boxed() ).boxed()
} }

1
templates/404.html Normal file
View file

@ -0,0 +1 @@
<p>Not found</p>

1
templates/500.html Normal file
View file

@ -0,0 +1 @@
<p>Internal server error</p>