This commit is contained in:
2023-08-29 21:34:00 +02:00
parent be9be4c9da
commit 6e8873f1b9
6 changed files with 22 additions and 24 deletions

View File

@@ -92,7 +92,7 @@ impl IntoResponse for Error {
), ),
models::Error::Query(error) => match error { models::Error::Query(error) => match error {
models::QueryError::NotFound { description } => { models::QueryError::NotFound { description } => {
(StatusCode::NOT_FOUND, view::ErrorPage::build(&description)) (StatusCode::NOT_FOUND, view::ErrorPage::build(description))
} }
_ => ( _ => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,

View File

@@ -64,9 +64,9 @@ impl fmt::Display for UriPath {
} }
} }
impl<'a> Into<&'a str> for &'a UriPath { impl<'a> From<&'a UriPath> for &'a str {
fn into(self) -> &'a str { fn from(val: &'a UriPath) -> Self {
self.0.as_str() val.0.as_str()
} }
} }

View File

@@ -118,16 +118,16 @@ impl From<sqlx::Error> for Error {
match &*code { match &*code {
// SQLITE_CONSTRAINT_FOREIGNKEY // SQLITE_CONSTRAINT_FOREIGNKEY
"787" => Error::Query(QueryError::ReferenceNotFound { "787" => Error::Query(QueryError::ReferenceNotFound {
description: format!("foreign key reference not found"), description: "foreign key reference not found".to_string(),
}), }),
// SQLITE_CONSTRAINT_UNIQUE // SQLITE_CONSTRAINT_UNIQUE
"2067" => Error::Query(QueryError::Duplicate { "2067" => Error::Query(QueryError::Duplicate {
description: format!("item with unique constraint already exists",), description: "item with unique constraint already exists".to_string(),
}), }),
_ => Error::Database(DatabaseError::Sql { _ => Error::Database(DatabaseError::Sql {
description: format!( description: format!(
"got error with unknown code: {}", "got error with unknown code: {}",
sqlite_error.to_string() sqlite_error
), ),
}), }),
} }
@@ -135,13 +135,13 @@ impl From<sqlx::Error> for Error {
Error::Database(DatabaseError::Sql { Error::Database(DatabaseError::Sql {
description: format!( description: format!(
"got error without code: {}", "got error without code: {}",
sqlite_error.to_string() sqlite_error
), ),
}) })
} }
} }
_ => Error::Database(DatabaseError::Sql { _ => Error::Database(DatabaseError::Sql {
description: format!("got unknown error: {}", value.to_string()), description: format!("got unknown error: {}", value),
}), }),
} }
} }

View File

@@ -29,7 +29,7 @@ impl Inventory {
.collect::<Result<Vec<Category>, Error>>()?; .collect::<Result<Vec<Category>, Error>>()?;
for category in &mut categories { for category in &mut categories {
category.populate_items(&ctx, &pool).await?; category.populate_items(ctx, pool).await?;
} }
Ok(Self { categories }) Ok(Self { categories })

View File

@@ -13,7 +13,7 @@ mod html;
mod routes; mod routes;
use routes::*; use routes::*;
fn get_referer<'a>(headers: &'a HeaderMap) -> Result<&'a str, Error> { fn get_referer(headers: &HeaderMap) -> Result<&str, Error> {
headers headers
.get("referer") .get("referer")
.ok_or(Error::Request(RequestError::RefererNotFound))? .ok_or(Error::Request(RequestError::RefererNotFound))?

View File

@@ -113,14 +113,14 @@ pub async fn icon() -> impl IntoResponse {
} }
pub async fn debug(headers: HeaderMap) -> impl IntoResponse { pub async fn debug(headers: HeaderMap) -> impl IntoResponse {
let out = {
{
let mut out = String::new(); let mut out = String::new();
for (key, value) in headers.iter() { for (key, value) in headers.iter() {
out.push_str(&format!("{}: {}\n", key, value.to_str().unwrap())); out.push_str(&format!("{}: {}\n", key, value.to_str().unwrap()));
} }
out out
}; }
out
} }
pub async fn inventory_active( pub async fn inventory_active(
Extension(current_user): Extension<models::user::User>, Extension(current_user): Extension<models::user::User>,
@@ -452,12 +452,10 @@ pub async fn trip_edit_attribute(
Form(trip_update): Form<TripUpdate>, Form(trip_update): Form<TripUpdate>,
) -> Result<Redirect, Error> { ) -> Result<Redirect, Error> {
let ctx = Context::build(current_user); let ctx = Context::build(current_user);
if attribute == models::trips::TripAttribute::Name { if attribute == models::trips::TripAttribute::Name && trip_update.new_value.is_empty() {
if trip_update.new_value.is_empty() { return Err(Error::Request(RequestError::EmptyFormElement {
return Err(Error::Request(RequestError::EmptyFormElement { name: "name".to_string(),
name: "name".to_string(), }));
}));
}
} }
models::trips::Trip::set_attribute( models::trips::Trip::set_attribute(
&ctx, &ctx,
@@ -479,7 +477,7 @@ pub async fn trip_item_set_state(
key: models::trips::TripItemStateKey, key: models::trips::TripItemStateKey,
value: bool, value: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
models::trips::TripItem::set_state(&ctx, &state.database_pool, trip_id, item_id, key, value) models::trips::TripItem::set_state(ctx, &state.database_pool, trip_id, item_id, key, value)
.await?; .await?;
Ok(()) Ok(())
} }
@@ -502,7 +500,7 @@ pub async fn trip_row(
trip_id, trip_id,
&item, &item,
models::inventory::InventoryItem::get_category_max_weight( models::inventory::InventoryItem::get_category_max_weight(
&ctx, ctx,
&state.database_pool, &state.database_pool,
item.item.category_id, item.item.category_id,
) )
@@ -902,9 +900,9 @@ pub async fn trips_types_edit_name(
.await?; .await?;
if !exists { if !exists {
return Err(Error::Request(RequestError::NotFound { Err(Error::Request(RequestError::NotFound {
message: format!("trip type with id {trip_type_id} not found"), message: format!("trip type with id {trip_type_id} not found"),
})); }))
} else { } else {
Ok(Redirect::to("/trips/types/")) Ok(Redirect::to("/trips/types/"))
} }