Apply suggestion

- remove the payload_error_handler in favor of a PayloadError::from

- merge the two match branch into one

- makes the accepted content type a const instead of recalculating it for every error
This commit is contained in:
Tamo 2021-10-06 11:49:34 +02:00
parent 37b267ffb3
commit 9a1e44dc78
No known key found for this signature in database
GPG key ID: 20CD8020AFA88D69
3 changed files with 38 additions and 39 deletions

View file

@ -6,6 +6,7 @@ use log::debug;
use meilisearch_lib::index_controller::{DocumentAdditionFormat, Update};
use meilisearch_lib::milli::update::IndexDocumentsMethod;
use meilisearch_lib::MeiliSearch;
use once_cell::sync::Lazy;
use serde::Deserialize;
use serde_json::Value;
use tokio::sync::mpsc;
@ -176,6 +177,13 @@ async fn document_addition(
body: Payload,
method: IndexDocumentsMethod,
) -> Result<HttpResponse, ResponseError> {
const ACCEPTED_CONTENT_TYPE: Lazy<Vec<String>> = Lazy::new(|| {
vec![
"application/json".to_string(),
"application/x-ndjson".to_string(),
"application/csv".to_string(),
]
});
let format = match content_type {
Some("application/json") => DocumentAdditionFormat::Json,
Some("application/x-ndjson") => DocumentAdditionFormat::Ndjson,
@ -183,21 +191,14 @@ async fn document_addition(
Some(other) => {
return Err(MeilisearchHttpError::InvalidContentType(
other.to_string(),
vec![
"application/json".to_string(),
"application/x-ndjson".to_string(),
"application/csv".to_string(),
],
ACCEPTED_CONTENT_TYPE.clone(),
)
.into())
}
None => {
return Err(MeilisearchHttpError::MissingContentType(vec![
"application/json".to_string(),
"application/x-ndjson".to_string(),
"application/csv".to_string(),
])
.into())
return Err(
MeilisearchHttpError::MissingContentType(ACCEPTED_CONTENT_TYPE.clone()).into(),
)
}
};