refactor: handle a few errors without panic!

This commit is contained in:
Muhammad Nauman Raza 2024-07-01 13:50:42 +01:00
parent 9fe47b2996
commit c8cbcbbc48
Signed by: devraza
GPG key ID: 91EAD6081011574B

View file

@ -34,7 +34,7 @@ pub struct User {
struct Webchain(sqlx::SqlitePool); struct Webchain(sqlx::SqlitePool);
#[get("/links")] #[get("/links")]
async fn links_template(mut db: Connection<Webchain>, cookies: &CookieJar<'_>) -> Template { async fn links_template(mut db: Connection<Webchain>, cookies: &CookieJar<'_>) -> Result<Template, &'static str> {
if let Some(cookie) = cookies.get("session") { if let Some(cookie) = cookies.get("session") {
let username = cookie.value(); let username = cookie.value();
@ -50,21 +50,13 @@ async fn links_template(mut db: Connection<Webchain>, cookies: &CookieJar<'_>) -
.await .await
.context("Failed to to retrieve stored credential"); .context("Failed to to retrieve stored credential");
let username: String = match row { let username: String = row.unwrap().expect("Couldn't retrieve the row").get("username");
Ok(Some(row)) => row.get("username"),
Ok(None) => {
panic!("Username not found");
}
Err(_) => {
panic!("Could not get the username from the database");
}
};
Template::render("links", context! { Ok(Template::render("links", context! {
username: username, username: username,
}) }))
} else { } else {
panic!("Please log in first!"); Err("Please log in first!")
} }
} }