From 67790fcb2f593dae9e57c0f6accf0335b5d80c43 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Mon, 16 Jan 2023 05:49:03 +0000 Subject: add error handlers --- src/main.rs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 7e61f15..63a8c10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use listenbrainz::raw::response::UserPlayingNowResponse; use rocket::fs::{relative, FileServer}; +use rocket::http::Status; use rocket::response::Responder; +use rocket::Request; use rocket_dyn_templates::{context, Template}; use serde::{Deserialize, Serialize}; @@ -55,10 +57,6 @@ impl ScrobbleData { async fn home() -> Template { let listenbrainz = listenbrainz::raw::Client::new(); let playingnow = listenbrainz.user_playing_now("celblossom").unwrap(); - println!( - "{}", - playingnow.payload.listens[0].track_metadata.artist_name - ); let listenbrainz_data = ScrobbleData::new(playingnow); println!("{}", listenbrainz_data.is_scrobbling); @@ -79,11 +77,40 @@ 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 status = format!("{}", status); + let message = "there was an error"; + Template::render( + "error", + context! { status: status, req: req.uri(), message: message }, + ) +} + #[tokio::main] async fn main() -> Result<(), rocket::Error> { let _rocket = rocket::build() .attach(Template::fairing()) .mount("/", routes![home, contact]) + .register("/", catchers![not_found, server_error, error]) .mount("/", FileServer::from(relative!("static"))) .launch() .await?; -- cgit