mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-27 03:41:32 +01:00
feat: refactored error handling
This commit is contained in:
@@ -13,10 +13,14 @@ pub async fn api_auth(
|
||||
mut request: Request,
|
||||
next: Next,
|
||||
) -> Result<Response, ApiError> {
|
||||
let api_key = extract_api_key(&request).ok_or(ApiError::AuthRequired)?;
|
||||
let api_key = extract_api_key(&request).ok_or(ApiError::Unauthorized(
|
||||
"You have to be authenticated".to_string(),
|
||||
))?;
|
||||
|
||||
let user = User::find_by_api_key(&api_key, &state.surreal_db_client).await?;
|
||||
let user = user.ok_or(ApiError::UserNotFound)?;
|
||||
let user = user.ok_or(ApiError::Unauthorized(
|
||||
"You have to be authenticated".to_string(),
|
||||
))?;
|
||||
|
||||
request.extensions_mut().insert(user);
|
||||
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
use crate::{error::ApiError, server::AppState, storage::types::file_info::FileInfo};
|
||||
use crate::{
|
||||
error::{ApiError, AppError},
|
||||
server::AppState,
|
||||
storage::types::file_info::FileInfo,
|
||||
};
|
||||
use axum::{extract::State, response::IntoResponse, Json};
|
||||
use axum_typed_multipart::{FieldData, TryFromMultipart, TypedMultipart};
|
||||
use serde_json::json;
|
||||
@@ -21,7 +25,9 @@ pub async fn upload_handler(
|
||||
info!("Received an upload request");
|
||||
|
||||
// Process the file upload
|
||||
let file_info = FileInfo::new(input.file, &state.surreal_db_client).await?;
|
||||
let file_info = FileInfo::new(input.file, &state.surreal_db_client)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
|
||||
// Prepare the response JSON
|
||||
let response = json!({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
error::{ApiError, AppError},
|
||||
ingress::types::ingress_input::{create_ingress_objects, IngressInput},
|
||||
server::AppState,
|
||||
storage::types::user::User,
|
||||
@@ -22,7 +22,7 @@ pub async fn ingress_handler(
|
||||
.map(|object| state.rabbitmq_producer.publish(object))
|
||||
.collect();
|
||||
|
||||
try_join_all(futures).await?;
|
||||
try_join_all(futures).await.map_err(AppError::from)?;
|
||||
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
use axum::{extract::State, http::StatusCode, response::IntoResponse};
|
||||
use minijinja::context;
|
||||
use tracing::{info, Instrument};
|
||||
use tracing::info;
|
||||
|
||||
use crate::{error::ApiError, server::AppState};
|
||||
use crate::{
|
||||
error::{ApiError, AppError},
|
||||
server::AppState,
|
||||
};
|
||||
|
||||
pub async fn queue_length_handler(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
info!("Getting queue length");
|
||||
|
||||
let queue_length = state.rabbitmq_consumer.get_queue_length().await?;
|
||||
let queue_length = state
|
||||
.rabbitmq_consumer
|
||||
.get_queue_length()
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
|
||||
info!("Queue length: {}", queue_length);
|
||||
|
||||
state
|
||||
.mailer
|
||||
.send_email_verification("per@starks.cloud", "1001010", &state.templates)?;
|
||||
.send_email_verification("per@starks.cloud", "1001010", &state.templates)
|
||||
.map_err(AppError::from)?;
|
||||
|
||||
// Return the queue length with a 200 OK status
|
||||
Ok((StatusCode::OK, queue_length.to_string()))
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use axum::{extract::State, response::Html};
|
||||
use axum::{extract::State, response::IntoResponse};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
error::{AppError, HtmlError},
|
||||
page_data,
|
||||
server::{routes::html::render_template, AppState},
|
||||
storage::types::user::User,
|
||||
@@ -19,10 +19,14 @@ page_data!(IndexData, "index/index.html", {
|
||||
pub async fn index_handler(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
) -> Result<Html<String>, ApiError> {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
info!("Displaying index page");
|
||||
|
||||
let queue_length = state.rabbitmq_consumer.get_queue_length().await?;
|
||||
let queue_length = state
|
||||
.rabbitmq_consumer
|
||||
.get_queue_length()
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
|
||||
|
||||
// let knowledge_entities = User::get_knowledge_entities(
|
||||
// &auth.current_user.clone().unwrap().id,
|
||||
@@ -38,8 +42,9 @@ pub async fn index_handler(
|
||||
queue_length,
|
||||
user: auth.current_user,
|
||||
},
|
||||
state.templates,
|
||||
)?;
|
||||
state.templates.clone(),
|
||||
)
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
|
||||
|
||||
Ok(output)
|
||||
Ok(output.into_response())
|
||||
}
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, Uri},
|
||||
response::{Html, IntoResponse, Redirect},
|
||||
Form,
|
||||
};
|
||||
use axum_htmx::{HxBoosted, HxRedirect};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use axum_typed_multipart::{FieldData, TryFromMultipart, TypedMultipart};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::Serialize;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use tempfile::NamedTempFile;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
error::{AppError, HtmlError},
|
||||
server::AppState,
|
||||
storage::types::{file_info::FileInfo, user::User},
|
||||
};
|
||||
|
||||
use super::{render_block, render_template};
|
||||
use super::render_template;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct PageData {
|
||||
@@ -29,13 +26,17 @@ struct PageData {
|
||||
pub async fn show_ingress_form(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
if !auth.is_authenticated() {
|
||||
return Ok(Redirect::to("/").into_response());
|
||||
}
|
||||
|
||||
Ok(render_template("ingress_form.html", PageData {}, state.templates)?.into_response())
|
||||
let output = render_template("ingress_form.html", PageData {}, state.templates.clone())
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
|
||||
|
||||
Ok(output.into_response())
|
||||
}
|
||||
|
||||
#[derive(Debug, TryFromMultipart)]
|
||||
pub struct IngressParams {
|
||||
pub content: Option<String>,
|
||||
@@ -49,8 +50,8 @@ pub async fn process_ingress_form(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
TypedMultipart(input): TypedMultipart<IngressParams>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let user = match auth.current_user {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let _user = match auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
@@ -60,7 +61,9 @@ pub async fn process_ingress_form(
|
||||
// Process files and create FileInfo objects
|
||||
let mut file_infos = Vec::new();
|
||||
for file in input.files {
|
||||
let file_info = FileInfo::new(file, &state.surreal_db_client).await?;
|
||||
let file_info = FileInfo::new(file, &state.surreal_db_client)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
|
||||
file_infos.push(file_info);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
response::Html,
|
||||
response::{Html, IntoResponse, Redirect},
|
||||
};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
@@ -9,7 +9,7 @@ use surrealdb::{engine::any::Any, Surreal};
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
error::ApiError, retrieval::query_helper::get_answer_with_references, server::AppState,
|
||||
error::HtmlError, retrieval::query_helper::get_answer_with_references, server::AppState,
|
||||
storage::types::user::User,
|
||||
};
|
||||
#[derive(Deserialize)]
|
||||
@@ -21,30 +21,22 @@ pub async fn search_result_handler(
|
||||
State(state): State<AppState>,
|
||||
Query(query): Query<SearchParams>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
) -> Result<Html<String>, ApiError> {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
info!("Displaying search results");
|
||||
|
||||
let user_id = auth.current_user.ok_or_else(|| ApiError::AuthRequired)?.id;
|
||||
let user = match auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
let answer = get_answer_with_references(
|
||||
&state.surreal_db_client,
|
||||
&state.openai_client,
|
||||
&query.query,
|
||||
&user_id,
|
||||
&user.id,
|
||||
)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
|
||||
Ok(Html(answer.content))
|
||||
// let output = state
|
||||
// .tera
|
||||
// .render(
|
||||
// "search_result.html",
|
||||
// &Context::from_value(
|
||||
// json!({"result": answer.content, "references": answer.references}),
|
||||
// )
|
||||
// .unwrap(),
|
||||
// )
|
||||
// .unwrap();
|
||||
|
||||
// Ok(output.into())
|
||||
Ok(Html(answer.content).into_response())
|
||||
}
|
||||
|
||||
@@ -9,7 +9,12 @@ use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
|
||||
use crate::{error::ApiError, page_data, server::AppState, storage::types::user::User};
|
||||
use crate::{
|
||||
error::{AppError, HtmlError},
|
||||
page_data,
|
||||
server::AppState,
|
||||
storage::types::user::User,
|
||||
};
|
||||
|
||||
use super::{render_block, render_template};
|
||||
|
||||
@@ -26,7 +31,7 @@ pub async fn show_signin_form(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
HxBoosted(boosted): HxBoosted,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
if auth.is_authenticated() {
|
||||
return Ok(Redirect::to("/").into_response());
|
||||
}
|
||||
@@ -35,13 +40,15 @@ pub async fn show_signin_form(
|
||||
ShowSignInForm::template_name(),
|
||||
"body",
|
||||
ShowSignInForm {},
|
||||
state.templates,
|
||||
)?,
|
||||
state.templates.clone(),
|
||||
)
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?,
|
||||
false => render_template(
|
||||
ShowSignInForm::template_name(),
|
||||
ShowSignInForm {},
|
||||
state.templates,
|
||||
)?,
|
||||
state.templates.clone(),
|
||||
)
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?,
|
||||
};
|
||||
|
||||
Ok(output.into_response())
|
||||
@@ -51,11 +58,11 @@ pub async fn authenticate_user(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
Form(form): Form<SignupParams>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match User::authenticate(form.email, form.password, &state.surreal_db_client).await {
|
||||
Ok(user) => user,
|
||||
Err(_) => {
|
||||
return Ok(Html("<p>Invalid email or password.</p>").into_response());
|
||||
return Ok(Html("<p>Incorrect email or password </p>").into_response());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -10,7 +10,11 @@ use axum_session_surreal::SessionSurrealPool;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
|
||||
use crate::{error::ApiError, server::AppState, storage::types::user::User};
|
||||
use crate::{
|
||||
error::{AppError, HtmlError},
|
||||
server::AppState,
|
||||
storage::types::user::User,
|
||||
};
|
||||
|
||||
use super::{render_block, render_template};
|
||||
|
||||
@@ -29,7 +33,7 @@ pub async fn show_signup_form(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
HxBoosted(boosted): HxBoosted,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
if auth.is_authenticated() {
|
||||
return Ok(Redirect::to("/").into_response());
|
||||
}
|
||||
@@ -38,9 +42,15 @@ pub async fn show_signup_form(
|
||||
"auth/signup_form.html",
|
||||
"body",
|
||||
PageData {},
|
||||
state.templates,
|
||||
)?,
|
||||
false => render_template("auth/signup_form.html", PageData {}, state.templates)?,
|
||||
state.templates.clone(),
|
||||
)
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?,
|
||||
false => render_template(
|
||||
"auth/signup_form.html",
|
||||
PageData {},
|
||||
state.templates.clone(),
|
||||
)
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?,
|
||||
};
|
||||
|
||||
Ok(output.into_response())
|
||||
@@ -50,7 +60,7 @@ pub async fn process_signup_and_show_verification(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
Form(form): Form<SignupParams>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match User::create_new(form.email, form.password, &state.surreal_db_client).await {
|
||||
Ok(user) => user,
|
||||
Err(_) => {
|
||||
|
||||
Reference in New Issue
Block a user