From 675081e9d28ab7f072e82446a52a8bd26382ed66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?cel=20=F0=9F=8C=B8?= Date: Thu, 16 Feb 2023 18:02:20 +0000 Subject: [PATCH] improve error handling --- src/error.rs | 18 ++++++++---------- src/main.rs | 11 +++++++---- src/scrobbles.rs | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/error.rs b/src/error.rs index 38802d3..890a148 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,29 +1,27 @@ -use rocket::Responder; +use rocket::{http::Status, Responder}; #[derive(Responder, Debug)] pub enum BlossomError { - #[response(status = 500)] - Reqwest(&'static str, #[response(ignore)] reqwest::Error), - #[response(status = 500)] - ListenBrainz(&'static str, #[response(ignore)] listenbrainz::Error), - #[response(status = 500)] - Skinnyverse(&'static str, #[response(ignore)] mastodon_async::Error), + Reqwest(Status, #[response(ignore)] reqwest::Error), + ListenBrainz(Status, #[response(ignore)] listenbrainz::Error), + Skinnyverse(Status, #[response(ignore)] mastodon_async::Error), + Unimplemented(Status), } impl From for BlossomError { fn from(e: reqwest::Error) -> Self { - BlossomError::Reqwest("reqwest error", e) + BlossomError::Reqwest(Status::new(500), e) } } impl From for BlossomError { fn from(e: listenbrainz::Error) -> Self { - BlossomError::ListenBrainz("listenbrainz error", e) + BlossomError::ListenBrainz(Status::new(500), e) } } impl From for BlossomError { fn from(e: mastodon_async::Error) -> Self { - BlossomError::Skinnyverse("skinnyverse error", e) + BlossomError::Skinnyverse(Status::new(500), e) } } diff --git a/src/main.rs b/src/main.rs index ba7bed0..1850b01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,10 @@ extern crate rocket; async fn home(clients: &State) -> Result { Ok(Template::render( "home", - context! { is_live: false, listenbrainz: scrobbles::get_now_playing(&clients.listenbrainz).await?, skweets: skweets::get_recents(&clients.skinnyverse).await? }, + context! { + is_live: false, + listenbrainz: scrobbles::get_now_playing(&clients.listenbrainz).await.unwrap_or_default(), + skweets: skweets::get_recents(&clients.skinnyverse).await.unwrap_or_default() }, )) } @@ -31,7 +34,7 @@ async fn contact() -> Template { #[get("/plants")] async fn plants() -> Result { - todo!() + Err(BlossomError::Unimplemented(Status::NotImplemented)) } #[catch(default)] @@ -44,7 +47,7 @@ fn catcher(status: Status, req: &Request) -> Template { } else if status.code == 501 { message = "it looks like this is not yet here!!!"; } else { - message = "there was an error"; + message = "idk i got bored"; } let status = format!("{}", status); Template::render( @@ -66,7 +69,7 @@ async fn main() -> Result<(), rocket::Error> { .attach(Template::custom(|engines| { engines.tera.autoescape_on(vec![]); })) - .mount("/", routes![home, contact]) + .mount("/", routes![home, contact, plants]) .register("/", catchers![catcher]) .mount("/", FileServer::from(relative!("static"))) .launch() diff --git a/src/scrobbles.rs b/src/scrobbles.rs index 07b1ad1..a6d4a54 100644 --- a/src/scrobbles.rs +++ b/src/scrobbles.rs @@ -10,7 +10,7 @@ pub async fn get_now_playing( Ok(NowPlayingData::new(playingnow)) } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Default)] pub struct NowPlayingData { pub is_scrobbling: bool, pub song: Option,