diff options
| author | 2023-01-16 05:49:03 +0000 | |
|---|---|---|
| committer | 2023-01-16 05:49:03 +0000 | |
| commit | 67790fcb2f593dae9e57c0f6accf0335b5d80c43 (patch) | |
| tree | 5408f5964b4bef6473b4dd059a4ae3dc41bcf924 /src/main.rs | |
| parent | 859f9fe9f8225e7bc3dcf769d3704ce3614db6bf (diff) | |
| download | blossom-67790fcb2f593dae9e57c0f6accf0335b5d80c43.tar.gz blossom-67790fcb2f593dae9e57c0f6accf0335b5d80c43.tar.bz2 blossom-67790fcb2f593dae9e57c0f6accf0335b5d80c43.zip  | |
add error handlers
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 35 | 
1 files changed, 31 insertions, 4 deletions
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?;  | 
