error handling, and setting result as return

This commit is contained in:
Per Stark
2024-11-23 15:35:44 +01:00
parent 8c0c10f58a
commit 380e2e1d01
3 changed files with 71 additions and 30 deletions
+9 -25
View File
@@ -1,41 +1,25 @@
use crate::{
error::ApiError,
ingress::types::ingress_input::{create_ingress_objects, IngressInput},
rabbitmq::publisher::RabbitMQProducer,
storage::db::SurrealDbClient,
};
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
use std::sync::Arc;
use tracing::{error, info};
use tracing::info;
pub async fn ingress_handler(
Extension(producer): Extension<Arc<RabbitMQProducer>>,
Extension(db_client): Extension<Arc<SurrealDbClient>>,
Json(input): Json<IngressInput>,
) -> impl IntoResponse {
) -> Result<impl IntoResponse, ApiError> {
info!("Received input: {:?}", input);
match create_ingress_objects(input, &db_client).await {
Ok(objects) => {
for object in objects {
match producer.publish(&object).await {
Ok(_) => {
info!("Message published successfully");
}
Err(e) => {
error!("Failed to publish message: {:?}", e);
return (
StatusCode::INTERNAL_SERVER_ERROR,
"Failed to publish message",
)
.into_response();
}
}
}
StatusCode::OK.into_response()
}
Err(e) => {
error!("Failed to process input: {:?}", e);
(StatusCode::INTERNAL_SERVER_ERROR, "Failed to process input").into_response()
}
let ingress_objects = create_ingress_objects(input, &db_client).await?;
for object in ingress_objects {
producer.publish(&object).await?;
}
Ok(StatusCode::OK)
}