send back errors if database is locked

This commit is contained in:
Khaïs COLIN 2025-10-19 19:34:28 +02:00
parent 3acd78e76c
commit 28ea420ecb
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -4,7 +4,7 @@ use askama::Template;
use axum::{ use axum::{
Router, Router,
extract::{MatchedPath, Path, State}, extract::{MatchedPath, Path, State},
http::Request, http::{Request, StatusCode},
response::Html, response::Html,
routing::{get, post}, routing::{get, post},
}; };
@ -169,67 +169,85 @@ async fn main() -> Result<(), std::io::Error> {
axum::serve(listener, app).await axum::serve(listener, app).await
} }
fn get_foods(conn: &ConnState) -> Vec<Food> { fn get_foods(conn: &ConnState) -> rusqlite::Result<Vec<Food>> {
let conn = conn.lock(); let conn = conn.lock();
let mut stmt = PreparedStatements::get_foods(&conn); let mut stmt = PreparedStatements::get_foods(&conn);
let foods: Vec<_> = stmt let foods: Vec<_> = stmt
.query_map((), Food::from_row) .query_map((), Food::from_row)?
.unwrap() .collect::<Result<_, _>>()?;
.collect::<Result<_, _>>()
.unwrap();
debug!(num_foods = foods.len()); debug!(num_foods = foods.len());
foods Ok(foods)
} }
fn get_sum(conn: &Arc<Mutex<Connection>>) -> i32 { fn get_sum(conn: &Arc<Mutex<Connection>>) -> rusqlite::Result<i32> {
let conn = conn.lock(); let conn = conn.lock();
let mut stmt = PreparedStatements::get_sum(&conn); let mut stmt = PreparedStatements::get_sum(&conn);
let sum = stmt.query_one((), |row| row.get(0)).unwrap(); let sum = stmt.query_one((), |row| row.get(0))?;
debug!(sum); debug!(sum);
sum Ok(sum)
} }
async fn root(State(conn): State<ConnState>) -> Html<String> { async fn root(State(conn): State<ConnState>) -> Result<Html<String>, StatusCode> {
let foods = get_foods(&conn); let foods = get_foods(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn); let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let index = IndexTemplate { foods, sum }; let index = IndexTemplate { foods, sum };
Html(index.render().unwrap()) Ok(Html(
index
.render()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
))
} }
fn do_increase(conn: &Arc<Mutex<Connection>>, id: i32) { fn do_increase(conn: &Arc<Mutex<Connection>>, id: i32) -> rusqlite::Result<()> {
let conn = conn.lock(); let conn = conn.lock();
let mut stmt = PreparedStatements::increase(&conn); let mut stmt = PreparedStatements::increase(&conn);
let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap(); let new: i32 = stmt.query_one((id,), |row| row.get(0))?;
debug!(id, new_serving_count = new, "increase"); debug!(id, new_serving_count = new, "increase");
Ok(())
} }
fn get_food(conn: &Arc<Mutex<Connection>>, id: i32) -> Food { fn get_food(conn: &Arc<Mutex<Connection>>, id: i32) -> rusqlite::Result<Food> {
let conn = conn.lock(); let conn = conn.lock();
let mut stmt = PreparedStatements::get_food(&conn); let mut stmt = PreparedStatements::get_food(&conn);
let food = stmt.query_one((id,), Food::from_row).unwrap(); let food = stmt.query_one((id,), Food::from_row)?;
debug!(?food); debug!(?food);
food Ok(food)
} }
async fn increase(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> { async fn increase(
do_increase(&conn, id); State(conn): State<Arc<Mutex<Connection>>>,
let food = get_food(&conn, id); Path(id): Path<i32>,
let sum = get_sum(&conn); ) -> Result<Html<String>, StatusCode> {
do_increase(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let update = FoodUpdateTemplate { food, sum }; let update = FoodUpdateTemplate { food, sum };
Html(update.render().unwrap()) Ok(Html(
update
.render()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
))
} }
fn do_decrease(conn: &Arc<Mutex<Connection>>, id: i32) { fn do_decrease(conn: &Arc<Mutex<Connection>>, id: i32) -> rusqlite::Result<()> {
let conn = conn.lock(); let conn = conn.lock();
let mut stmt = PreparedStatements::decrease(&conn); let mut stmt = PreparedStatements::decrease(&conn);
let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap(); let new: i32 = stmt.query_one((id,), |row| row.get(0))?;
debug!(id, new_serving_count = new, "decrease"); debug!(id, new_serving_count = new, "decrease");
Ok(())
} }
async fn decrease(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> { async fn decrease(
do_decrease(&conn, id); State(conn): State<Arc<Mutex<Connection>>>,
let food = get_food(&conn, id); Path(id): Path<i32>,
let sum = get_sum(&conn); ) -> Result<Html<String>, StatusCode> {
do_decrease(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let update = FoodUpdateTemplate { food, sum }; let update = FoodUpdateTemplate { food, sum };
Html(update.render().unwrap()) Ok(Html(
update
.render()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
))
} }