From 8d176d8c076a5e71c210ee7464512b983659e1a0 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Mon, 16 Jan 2023 05:54:09 +0000 Subject: refactor error handler (LOL) --- src/main.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 63a8c10..6efade0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,28 +77,19 @@ async fn contact() -> Template { // 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)] 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 message = "there was an error"; Template::render( "error", context! { status: status, req: req.uri(), message: message }, @@ -110,7 +101,7 @@ async fn main() -> Result<(), rocket::Error> { let _rocket = rocket::build() .attach(Template::fairing()) .mount("/", routes![home, contact]) - .register("/", catchers![not_found, server_error, error]) + .register("/", catchers![error]) .mount("/", FileServer::from(relative!("static"))) .launch() .await?; -- cgit