error handling, and setting result as return

This commit is contained in:
Per Stark
2024-11-23 15:35:44 +01:00
parent 6f077e0190
commit d731e69bf9
3 changed files with 71 additions and 30 deletions

View File

@@ -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()
}
}