improve error handling

This commit is contained in:
cel 🌸 2023-02-16 18:02:20 +00:00
parent 059cf500e7
commit 675081e9d2
Signed by: cel
GPG Key ID: 48E29AF13B5F1349
3 changed files with 16 additions and 15 deletions

View File

@ -1,29 +1,27 @@
use rocket::Responder; use rocket::{http::Status, Responder};
#[derive(Responder, Debug)] #[derive(Responder, Debug)]
pub enum BlossomError { pub enum BlossomError {
#[response(status = 500)] Reqwest(Status, #[response(ignore)] reqwest::Error),
Reqwest(&'static str, #[response(ignore)] reqwest::Error), ListenBrainz(Status, #[response(ignore)] listenbrainz::Error),
#[response(status = 500)] Skinnyverse(Status, #[response(ignore)] mastodon_async::Error),
ListenBrainz(&'static str, #[response(ignore)] listenbrainz::Error), Unimplemented(Status),
#[response(status = 500)]
Skinnyverse(&'static str, #[response(ignore)] mastodon_async::Error),
} }
impl From<reqwest::Error> for BlossomError { impl From<reqwest::Error> for BlossomError {
fn from(e: reqwest::Error) -> Self { fn from(e: reqwest::Error) -> Self {
BlossomError::Reqwest("reqwest error", e) BlossomError::Reqwest(Status::new(500), e)
} }
} }
impl From<listenbrainz::Error> for BlossomError { impl From<listenbrainz::Error> for BlossomError {
fn from(e: listenbrainz::Error) -> Self { fn from(e: listenbrainz::Error) -> Self {
BlossomError::ListenBrainz("listenbrainz error", e) BlossomError::ListenBrainz(Status::new(500), e)
} }
} }
impl From<mastodon_async::Error> for BlossomError { impl From<mastodon_async::Error> for BlossomError {
fn from(e: mastodon_async::Error) -> Self { fn from(e: mastodon_async::Error) -> Self {
BlossomError::Skinnyverse("skinnyverse error", e) BlossomError::Skinnyverse(Status::new(500), e)
} }
} }

View File

@ -20,7 +20,10 @@ extern crate rocket;
async fn home(clients: &State<Clients>) -> Result<Template, BlossomError> { async fn home(clients: &State<Clients>) -> Result<Template, BlossomError> {
Ok(Template::render( Ok(Template::render(
"home", "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")] #[get("/plants")]
async fn plants() -> Result<Template, BlossomError> { async fn plants() -> Result<Template, BlossomError> {
todo!() Err(BlossomError::Unimplemented(Status::NotImplemented))
} }
#[catch(default)] #[catch(default)]
@ -44,7 +47,7 @@ fn catcher(status: Status, req: &Request) -> Template {
} else if status.code == 501 { } else if status.code == 501 {
message = "it looks like this is not yet here!!!"; message = "it looks like this is not yet here!!!";
} else { } else {
message = "there was an error"; message = "idk i got bored";
} }
let status = format!("{}", status); let status = format!("{}", status);
Template::render( Template::render(
@ -66,7 +69,7 @@ async fn main() -> Result<(), rocket::Error> {
.attach(Template::custom(|engines| { .attach(Template::custom(|engines| {
engines.tera.autoescape_on(vec![]); engines.tera.autoescape_on(vec![]);
})) }))
.mount("/", routes![home, contact]) .mount("/", routes![home, contact, plants])
.register("/", catchers![catcher]) .register("/", catchers![catcher])
.mount("/", FileServer::from(relative!("static"))) .mount("/", FileServer::from(relative!("static")))
.launch() .launch()

View File

@ -10,7 +10,7 @@ pub async fn get_now_playing(
Ok(NowPlayingData::new(playingnow)) Ok(NowPlayingData::new(playingnow))
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Default)]
pub struct NowPlayingData { pub struct NowPlayingData {
pub is_scrobbling: bool, pub is_scrobbling: bool,
pub song: Option<String>, pub song: Option<String>,