Error handling refactor and 500 page/message templates

This commit is contained in:
Alphonse Paix
2025-09-20 04:06:48 +02:00
parent 7971095227
commit d85879a004
14 changed files with 223 additions and 201 deletions

View File

@@ -1,5 +1,5 @@
use crate::{
authentication::{self, AuthenticatedUser, Credentials, validate_credentials},
authentication::{self, AuthError, AuthenticatedUser, Credentials, validate_credentials},
routes::{AdminError, AppError},
startup::AppState,
templates::MessageTemplate,
@@ -35,11 +35,14 @@ pub async fn change_password(
"You entered two different passwords - the field values must match.".to_string(),
)
.into())
} else if validate_credentials(credentials, &connection_pool)
.await
.is_err()
{
Err(AdminError::ChangePassword("The current password is incorrect.".to_string()).into())
} else if let Err(e) = validate_credentials(credentials, &connection_pool).await {
match e {
AuthError::UnexpectedError(error) => Err(AdminError::UnexpectedError(error).into()),
AuthError::InvalidCredentials(_) => Err(AdminError::ChangePassword(
"The current password is incorrect.".to_string(),
)
.into()),
}
} else if let Err(e) = verify_password(form.new_password.expose_secret()) {
Err(AdminError::ChangePassword(e).into())
} else {

View File

@@ -84,7 +84,7 @@ pub async fn insert_post(
title: &str,
content: &str,
author: &Uuid,
) -> Result<Uuid, AdminError> {
) -> Result<Uuid, sqlx::Error> {
let post_id = Uuid::new_v4();
let query = sqlx::query!(
r#"
@@ -97,10 +97,7 @@ pub async fn insert_post(
content,
Utc::now()
);
transaction
.execute(query)
.await
.map_err(|e| AdminError::UnexpectedError(e.into()))?;
transaction.execute(query).await?;
Ok(post_id)
}