aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2023-01-16 05:49:03 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2023-01-16 05:49:03 +0000
commit67790fcb2f593dae9e57c0f6accf0335b5d80c43 (patch)
tree5408f5964b4bef6473b4dd059a4ae3dc41bcf924
parent859f9fe9f8225e7bc3dcf769d3704ce3614db6bf (diff)
downloadblossom-67790fcb2f593dae9e57c0f6accf0335b5d80c43.tar.gz
blossom-67790fcb2f593dae9e57c0f6accf0335b5d80c43.tar.bz2
blossom-67790fcb2f593dae9e57c0f6accf0335b5d80c43.zip
add error handlers
-rw-r--r--src/main.rs35
-rw-r--r--templates/error.html.tera10
2 files changed, 41 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?;
diff --git a/templates/error.html.tera b/templates/error.html.tera
new file mode 100644
index 0000000..5a4cc17
--- /dev/null
+++ b/templates/error.html.tera
@@ -0,0 +1,10 @@
+{% extends "base" %}
+
+{% block content %}
+
+<div class="content panel">
+ <h2>Error {{ status }}: {{ req }}</h2>
+ <h3>{{ message }}</h3>
+</div>
+
+{% endblock %}