refactor error handler (LOL)

This commit is contained in:
cel 🌸 2023-01-16 05:54:09 +00:00
parent 67790fcb2f
commit 8d176d8c07
Signed by: cel
GPG Key ID: 48E29AF13B5F1349
1 changed files with 11 additions and 20 deletions

View File

@ -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?;