enable response error for documents routes

This commit is contained in:
marin postma 2021-06-15 16:22:06 +02:00
parent 5c52a1393f
commit 8afbb9c462
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9

View File

@ -61,15 +61,10 @@ async fn get_document(
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let index = path.index_uid.clone(); let index = path.index_uid.clone();
let id = path.document_id.clone(); let id = path.document_id.clone();
match data let document = data
.retrieve_document(index, id, None as Option<Vec<String>>) .retrieve_document(index, id, None as Option<Vec<String>>)
.await .await?;
{ Ok(HttpResponse::Ok().json(document))
Ok(document) => Ok(HttpResponse::Ok().json(document)),
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[delete( #[delete(
@ -80,17 +75,10 @@ async fn delete_document(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<DocumentParam>, path: web::Path<DocumentParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data let update_status = data
.delete_documents(path.index_uid.clone(), vec![path.document_id.clone()]) .delete_documents(path.index_uid.clone(), vec![path.document_id.clone()])
.await .await?;
{ Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
Ok(update_status) => Ok(
HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))
),
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -118,20 +106,15 @@ async fn get_all_documents(
Some(names) Some(names)
}); });
match data let documents = data
.retrieve_documents( .retrieve_documents(
path.index_uid.clone(), path.index_uid.clone(),
params.offset.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_OFFSET), params.offset.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_OFFSET),
params.limit.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_LIMIT), params.limit.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_LIMIT),
attributes_to_retrieve, attributes_to_retrieve,
) )
.await .await?;
{ Ok(HttpResponse::Ok().json(documents))
Ok(documents) => Ok(HttpResponse::Ok().json(documents)),
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -149,7 +132,7 @@ async fn add_documents(
params: web::Query<UpdateDocumentsQuery>, params: web::Query<UpdateDocumentsQuery>,
body: Payload, body: Payload,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let addition_result = data let update_status = data
.add_documents( .add_documents(
path.into_inner().index_uid, path.into_inner().index_uid,
IndexDocumentsMethod::ReplaceDocuments, IndexDocumentsMethod::ReplaceDocuments,
@ -157,16 +140,9 @@ async fn add_documents(
body, body,
params.primary_key.clone(), params.primary_key.clone(),
) )
.await; .await?;
match addition_result { Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
Ok(update_status) => Ok(
HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))
),
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
/// Default route for adding documents, this should return an error and redirect to the documentation /// Default route for adding documents, this should return an error and redirect to the documentation
@ -200,7 +176,7 @@ async fn update_documents(
params: web::Query<UpdateDocumentsQuery>, params: web::Query<UpdateDocumentsQuery>,
body: web::Payload, body: web::Payload,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let addition_result = data let update = data
.add_documents( .add_documents(
path.into_inner().index_uid, path.into_inner().index_uid,
IndexDocumentsMethod::UpdateDocuments, IndexDocumentsMethod::UpdateDocuments,
@ -208,16 +184,9 @@ async fn update_documents(
body, body,
params.primary_key.clone(), params.primary_key.clone(),
) )
.await; .await?;
match addition_result { Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update.id() })))
Ok(update) => {
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update.id() })))
}
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
#[post( #[post(
@ -238,14 +207,8 @@ async fn delete_documents(
}) })
.collect(); .collect();
match data.delete_documents(path.index_uid.clone(), ids).await { let update_status = data.delete_documents(path.index_uid.clone(), ids).await?;
Ok(update_status) => Ok( Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))
),
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }
/// delete all documents /// delete all documents
@ -254,12 +217,6 @@ async fn clear_all_documents(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data.clear_documents(path.index_uid.clone()).await { let update_status = data.clear_documents(path.index_uid.clone()).await?;
Ok(update_status) => Ok( Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))
),
Err(e) => {
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
}
}
} }