mirror of
https://github.com/perstarkse/minne.git
synced 2026-05-12 10:50:10 +02:00
error handling, and setting result as return
This commit is contained in:
43
src/error.rs
43
src/error.rs
@@ -1,10 +1,11 @@
|
||||
use async_openai::error::OpenAIError;
|
||||
use axum::{http::StatusCode, response::IntoResponse, Json};
|
||||
use serde_json::json;
|
||||
use thiserror::Error;
|
||||
use tokio::task::JoinError;
|
||||
|
||||
use crate::{ingress::types::ingress_input::IngressContentError, rabbitmq::RabbitMQError};
|
||||
|
||||
/// Error types for processing `TextContent`.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ProcessingError {
|
||||
#[error("SurrealDb error: {0}")]
|
||||
@@ -37,3 +38,43 @@ pub enum IngressConsumerError {
|
||||
#[error("Ingress content error: {0}")]
|
||||
IngressContent(#[from] IngressContentError),
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ApiError {
|
||||
#[error("Processing error: {0}")]
|
||||
ProcessingError(#[from] ProcessingError),
|
||||
#[error("Ingress content error: {0}")]
|
||||
IngressContentError(#[from] IngressContentError),
|
||||
#[error("Publishing error: {0}")]
|
||||
PublishingError(String),
|
||||
#[error("Database error: {0}")]
|
||||
DatabaseError(String),
|
||||
#[error("Query error: {0}")]
|
||||
QueryError(String),
|
||||
#[error("RabbitMQ error: {0}")]
|
||||
RabbitMQError(#[from] RabbitMQError),
|
||||
}
|
||||
|
||||
impl IntoResponse for ApiError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
let (status, error_message) = match &self {
|
||||
ApiError::ProcessingError(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
|
||||
ApiError::PublishingError(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
|
||||
ApiError::DatabaseError(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
|
||||
ApiError::QueryError(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
ApiError::IngressContentError(_) => {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string())
|
||||
}
|
||||
ApiError::RabbitMQError(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
|
||||
};
|
||||
|
||||
(
|
||||
status,
|
||||
Json(json!({
|
||||
"error": error_message,
|
||||
"status": "error"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user