refactor error handler (LOL)
This commit is contained in:
parent
67790fcb2f
commit
8d176d8c07
31
src/main.rs
31
src/main.rs
|
@ -77,28 +77,19 @@ async fn contact() -> Template {
|
||||||
// posts
|
// posts
|
||||||
// }
|
// }
|
||||||
|
|
||||||
#[catch(404)]
|
|
||||||
fn not_found(req: &Request) -> Template {
|
|
||||||
let message = "i either haven't built this page yet or it looks like you're a little lost";
|
|
||||||
Template::render(
|
|
||||||
"error",
|
|
||||||
context! { status: "404", req: req.uri(), message: message },
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[catch(501)]
|
|
||||||
fn server_error(req: &Request) -> Template {
|
|
||||||
let message = "it looks like this is not yet here!!!";
|
|
||||||
Template::render(
|
|
||||||
"error",
|
|
||||||
context! { status: "501", req: req.uri(), message: message },
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[catch(default)]
|
#[catch(default)]
|
||||||
fn error(status: Status, req: &Request) -> Template {
|
fn error(status: Status, req: &Request) -> Template {
|
||||||
|
let message;
|
||||||
|
if status.code == 404 {
|
||||||
|
message = "i either haven't built this page yet or it looks like you're a little lost";
|
||||||
|
} else if status.code == 500 {
|
||||||
|
message = "omg the server went kaputt!!";
|
||||||
|
} else if status.code == 501 {
|
||||||
|
message = "it looks like this is not yet here!!!";
|
||||||
|
} else {
|
||||||
|
message = "there was an error";
|
||||||
|
}
|
||||||
let status = format!("{}", status);
|
let status = format!("{}", status);
|
||||||
let message = "there was an error";
|
|
||||||
Template::render(
|
Template::render(
|
||||||
"error",
|
"error",
|
||||||
context! { status: status, req: req.uri(), message: message },
|
context! { status: status, req: req.uri(), message: message },
|
||||||
|
@ -110,7 +101,7 @@ async fn main() -> Result<(), rocket::Error> {
|
||||||
let _rocket = rocket::build()
|
let _rocket = rocket::build()
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", routes![home, contact])
|
.mount("/", routes![home, contact])
|
||||||
.register("/", catchers![not_found, server_error, error])
|
.register("/", catchers![error])
|
||||||
.mount("/", FileServer::from(relative!("static")))
|
.mount("/", FileServer::from(relative!("static")))
|
||||||
.launch()
|
.launch()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
Loading…
Reference in New Issue