mirror of
https://github.com/perstarkse/minne.git
synced 2026-07-19 03:01:20 +02:00
feat: refactored error handling
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
use super::ingress_object::IngressObject;
|
||||
use crate::storage::{
|
||||
db::{get_item, SurrealDbClient},
|
||||
types::file_info::FileInfo,
|
||||
use crate::{
|
||||
error::AppError,
|
||||
storage::{
|
||||
db::{get_item, SurrealDbClient},
|
||||
types::file_info::FileInfo,
|
||||
},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
use tracing::info;
|
||||
use url::Url;
|
||||
|
||||
@@ -17,34 +19,6 @@ pub struct IngressInput {
|
||||
pub files: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
/// Error types for processing ingress content.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum IngressContentError {
|
||||
#[error("IO error occurred: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("UTF-8 conversion error: {0}")]
|
||||
Utf8(#[from] std::string::FromUtf8Error),
|
||||
|
||||
#[error("SurrealDb error: {0}")]
|
||||
SurrealDbError(#[from] surrealdb::Error),
|
||||
|
||||
#[error("MIME type detection failed for input: {0}")]
|
||||
MimeDetection(String),
|
||||
|
||||
#[error("Unsupported MIME type: {0}")]
|
||||
UnsupportedMime(String),
|
||||
|
||||
#[error("URL parse error: {0}")]
|
||||
UrlParse(#[from] url::ParseError),
|
||||
|
||||
#[error("UUID parse error: {0}")]
|
||||
UuidParse(#[from] uuid::Error),
|
||||
|
||||
#[error("Redis error: {0}")]
|
||||
RedisError(String),
|
||||
}
|
||||
|
||||
/// Function to create ingress objects from input.
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -57,7 +31,7 @@ pub async fn create_ingress_objects(
|
||||
input: IngressInput,
|
||||
db_client: &SurrealDbClient,
|
||||
user_id: &str,
|
||||
) -> Result<Vec<IngressObject>, IngressContentError> {
|
||||
) -> Result<Vec<IngressObject>, AppError> {
|
||||
// Initialize list
|
||||
let mut object_list = Vec::new();
|
||||
|
||||
@@ -103,7 +77,7 @@ pub async fn create_ingress_objects(
|
||||
|
||||
// If no objects are constructed, we return Err
|
||||
if object_list.is_empty() {
|
||||
return Err(IngressContentError::MimeDetection(
|
||||
return Err(AppError::NotFound(
|
||||
"No valid content or files provided".into(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use crate::storage::types::{file_info::FileInfo, text_content::TextContent};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
storage::types::{file_info::FileInfo, text_content::TextContent},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::ingress_input::IngressContentError;
|
||||
|
||||
/// Knowledge object type, containing the content or reference to it, as well as metadata
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub enum IngressObject {
|
||||
@@ -34,7 +35,7 @@ impl IngressObject {
|
||||
///
|
||||
/// # Returns
|
||||
/// `TextContent` - An object containing a text representation of the object, could be a scraped URL, parsed PDF, etc.
|
||||
pub async fn to_text_content(&self) -> Result<TextContent, IngressContentError> {
|
||||
pub async fn to_text_content(&self) -> Result<TextContent, AppError> {
|
||||
match self {
|
||||
IngressObject::Url {
|
||||
url,
|
||||
@@ -82,12 +83,12 @@ impl IngressObject {
|
||||
}
|
||||
|
||||
/// Fetches and extracts text from a URL.
|
||||
async fn fetch_text_from_url(_url: &str) -> Result<String, IngressContentError> {
|
||||
async fn fetch_text_from_url(_url: &str) -> Result<String, AppError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
/// Extracts text from a file based on its MIME type.
|
||||
async fn extract_text_from_file(file_info: &FileInfo) -> Result<String, IngressContentError> {
|
||||
async fn extract_text_from_file(file_info: &FileInfo) -> Result<String, AppError> {
|
||||
match file_info.mime_type.as_str() {
|
||||
"text/plain" => {
|
||||
// Read the file and return its content
|
||||
@@ -101,15 +102,11 @@ impl IngressObject {
|
||||
}
|
||||
"application/pdf" => {
|
||||
// TODO: Implement PDF text extraction using a crate like `pdf-extract` or `lopdf`
|
||||
Err(IngressContentError::UnsupportedMime(
|
||||
file_info.mime_type.clone(),
|
||||
))
|
||||
Err(AppError::NotFound(file_info.mime_type.clone()))
|
||||
}
|
||||
"image/png" | "image/jpeg" => {
|
||||
// TODO: Implement OCR on image using a crate like `tesseract`
|
||||
Err(IngressContentError::UnsupportedMime(
|
||||
file_info.mime_type.clone(),
|
||||
))
|
||||
Err(AppError::NotFound(file_info.mime_type.clone()))
|
||||
}
|
||||
"application/octet-stream" => {
|
||||
let content = tokio::fs::read_to_string(&file_info.path).await?;
|
||||
@@ -120,9 +117,7 @@ impl IngressObject {
|
||||
Ok(content)
|
||||
}
|
||||
// Handle other MIME types as needed
|
||||
_ => Err(IngressContentError::UnsupportedMime(
|
||||
file_info.mime_type.clone(),
|
||||
)),
|
||||
_ => Err(AppError::NotFound(file_info.mime_type.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user