feat: refactored error handling

This commit is contained in:
Per Stark
2025-01-01 23:26:41 +01:00
parent a3bb73646c
commit 9976fef5a3
25 changed files with 439 additions and 293 deletions

View File

@@ -1,7 +1,7 @@
use axum::{
extract::State,
http::{Response, StatusCode, Uri},
response::{Html, IntoResponse, Redirect},
http::{StatusCode, Uri},
response::{IntoResponse, Redirect},
};
use axum_htmx::HxRedirect;
use axum_session_auth::AuthSession;
@@ -9,7 +9,7 @@ use axum_session_surreal::SessionSurrealPool;
use surrealdb::{engine::any::Any, Surreal};
use crate::{
error::ApiError,
error::{AppError, HtmlError},
page_data,
server::{routes::html::render_template, AppState},
storage::{db::delete_item, types::user::User},
@@ -24,7 +24,7 @@ page_data!(AccountData, "auth/account.html", {
pub async fn show_account_page(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
@@ -34,8 +34,9 @@ pub async fn show_account_page(
let output = render_template(
AccountData::template_name(),
AccountData { user },
state.templates,
)?;
state.templates.clone(),
)
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
Ok(output.into_response())
}
@@ -43,12 +44,17 @@ pub async fn show_account_page(
pub async fn set_api_key(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = auth.current_user.as_ref().ok_or(ApiError::AuthRequired)?;
let user = match &auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/").into_response()),
};
// Generate and set the API key
let api_key = User::set_api_key(&user.id, &state.surreal_db_client).await?;
let api_key = User::set_api_key(&user.id, &state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
auth.cache_clear_user(user.id.to_string());
@@ -63,8 +69,9 @@ pub async fn set_api_key(
AccountData::template_name(),
"api_key_section",
AccountData { user: updated_user },
state.templates,
)?;
state.templates.clone(),
)
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
Ok(output.into_response())
}
@@ -72,11 +79,16 @@ pub async fn set_api_key(
pub async fn delete_account(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = auth.current_user.as_ref().ok_or(ApiError::AuthRequired)?;
let user = match &auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/").into_response()),
};
delete_item::<User>(&state.surreal_db_client, &user.id).await?;
delete_item::<User>(&state.surreal_db_client, &user.id)
.await
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
auth.logout_user();