enable response error for index routes

This commit is contained in:
marin postma 2021-06-15 16:02:49 +02:00
parent 58f9974be4
commit d1550670a8
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9

View File

@ -3,9 +3,9 @@ use actix_web::{web, HttpResponse};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{IndexParam, UpdateStatusResponse};
use crate::error::ResponseError; use crate::error::ResponseError;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use super::{UpdateStatusResponse, IndexParam};
use crate::Data; use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {
@ -20,12 +20,8 @@ pub fn services(cfg: &mut web::ServiceConfig) {
#[get("/indexes", wrap = "Authentication::Private")] #[get("/indexes", wrap = "Authentication::Private")]
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
match data.list_indexes().await { let indexes = data.list_indexes().await?;
Ok(indexes) => Ok(HttpResponse::Ok().json(indexes)), Ok(HttpResponse::Ok().json(indexes))
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[get("/indexes/{index_uid}", wrap = "Authentication::Private")] #[get("/indexes/{index_uid}", wrap = "Authentication::Private")]
@ -33,12 +29,8 @@ async fn get_index(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data.index(path.index_uid.clone()).await { let meta = data.index(path.index_uid.clone()).await?;
Ok(meta) => Ok(HttpResponse::Ok().json(meta)), Ok(HttpResponse::Ok().json(meta))
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -54,12 +46,8 @@ async fn create_index(
body: web::Json<IndexCreateRequest>, body: web::Json<IndexCreateRequest>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let body = body.into_inner(); let body = body.into_inner();
match data.create_index(body.uid, body.primary_key).await { let meta = data.create_index(body.uid, body.primary_key).await?;
Ok(meta) => Ok(HttpResponse::Ok().json(meta)), Ok(HttpResponse::Ok().json(meta))
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -86,15 +74,10 @@ async fn update_index(
body: web::Json<UpdateIndexRequest>, body: web::Json<UpdateIndexRequest>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let body = body.into_inner(); let body = body.into_inner();
match data let meta = data
.update_index(path.into_inner().index_uid, body.primary_key, body.uid) .update_index(path.into_inner().index_uid, body.primary_key, body.uid)
.await .await?;
{ Ok(HttpResponse::Ok().json(meta))
Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[delete("/indexes/{index_uid}", wrap = "Authentication::Private")] #[delete("/indexes/{index_uid}", wrap = "Authentication::Private")]
@ -102,12 +85,8 @@ async fn delete_index(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data.delete_index(path.index_uid.clone()).await { data.delete_index(path.index_uid.clone()).await?;
Ok(_) => Ok(HttpResponse::NoContent().finish()), Ok(HttpResponse::NoContent().finish())
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -125,18 +104,11 @@ async fn get_update_status(
path: web::Path<UpdateParam>, path: web::Path<UpdateParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let params = path.into_inner(); let params = path.into_inner();
let result = data let meta = data
.get_update_status(params.index_uid, params.update_id) .get_update_status(params.index_uid, params.update_id)
.await; .await?;
match result { let meta = UpdateStatusResponse::from(meta);
Ok(meta) => { Ok(HttpResponse::Ok().json(meta))
let meta = UpdateStatusResponse::from(meta);
Ok(HttpResponse::Ok().json(meta))
},
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[get("/indexes/{index_uid}/updates", wrap = "Authentication::Private")] #[get("/indexes/{index_uid}/updates", wrap = "Authentication::Private")]
@ -144,18 +116,11 @@ async fn get_all_updates_status(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let result = data.get_updates_status(path.into_inner().index_uid).await; let metas = data.get_updates_status(path.into_inner().index_uid).await?;
match result { let metas = metas
Ok(metas) => { .into_iter()
let metas = metas .map(UpdateStatusResponse::from)
.into_iter() .collect::<Vec<_>>();
.map(UpdateStatusResponse::from)
.collect::<Vec<_>>();
Ok(HttpResponse::Ok().json(metas)) Ok(HttpResponse::Ok().json(metas))
},
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }