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