refactor: handle a few errors without panic!
This commit is contained in:
parent
9fe47b2996
commit
c8cbcbbc48
18
src/main.rs
18
src/main.rs
|
@ -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!")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue