From 5d5615ef453001996b69ce1639df2d4154694741 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 13 Dec 2022 15:07:35 +0100 Subject: [PATCH] Rename the ReceivePayload error variant --- meilisearch/src/error.rs | 6 +-- meilisearch/src/routes/indexes/documents.rs | 50 +++++++++------------ 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/meilisearch/src/error.rs b/meilisearch/src/error.rs index 53b16f9f5..ce3d383c3 100644 --- a/meilisearch/src/error.rs +++ b/meilisearch/src/error.rs @@ -95,8 +95,8 @@ pub enum PayloadError { MalformedPayload(serde_json::error::Error), #[error("A json payload is missing.")] MissingPayload, - #[error("Error while writing the playload to disk: `{0}`.")] - ReceivePayloadErr(Box), + #[error("Error while receiving the playload. `{0}`.")] + ReceivePayload(Box), } impl ErrorCode for PayloadError { @@ -128,7 +128,7 @@ impl ErrorCode for PayloadError { }, PayloadError::MissingPayload => Code::MissingPayload, PayloadError::MalformedPayload(_) => Code::MalformedPayload, - PayloadError::ReceivePayloadErr(_) => Code::Internal, + PayloadError::ReceivePayload(_) => Code::Internal, } } } diff --git a/meilisearch/src/routes/indexes/documents.rs b/meilisearch/src/routes/indexes/documents.rs index 5353c1506..fa769a8c7 100644 --- a/meilisearch/src/routes/indexes/documents.rs +++ b/meilisearch/src/routes/indexes/documents.rs @@ -26,7 +26,7 @@ use tokio::io::{AsyncSeekExt, AsyncWriteExt, BufWriter}; use crate::analytics::{Analytics, DocumentDeletionKind}; use crate::error::MeilisearchHttpError; -use crate::error::PayloadError::ReceivePayloadErr; +use crate::error::PayloadError::ReceivePayload; use crate::extractors::authentication::policies::*; use crate::extractors::authentication::GuardedData; use crate::extractors::payload::Payload; @@ -232,33 +232,29 @@ async fn document_addition( let (uuid, mut update_file) = index_scheduler.create_update_file()?; let temp_file = match tempfile() { - Ok(temp_file) => temp_file, - Err(e) => { - return Err(MeilisearchHttpError::Payload(ReceivePayloadErr(Box::new(e)))); - } + Ok(file) => file, + Err(e) => return Err(MeilisearchHttpError::Payload(ReceivePayload(Box::new(e)))), }; let async_file = File::from_std(temp_file); let mut buffer = BufWriter::new(async_file); let mut buffer_write_size: usize = 0; - while let Some(bytes) = body.next().await { - let byte = &bytes?; + while let Some(result) = body.next().await { + let byte = result?; if byte.is_empty() && buffer_write_size == 0 { return Err(MeilisearchHttpError::MissingPayload(format)); } - match buffer.write_all(byte).await { + match buffer.write_all(&byte).await { Ok(()) => buffer_write_size += 1, - Err(e) => { - return Err(MeilisearchHttpError::Payload(ReceivePayloadErr(Box::new(e)))); - } - }; + Err(e) => return Err(MeilisearchHttpError::Payload(ReceivePayload(Box::new(e)))), + } } if let Err(e) = buffer.flush().await { - return Err(MeilisearchHttpError::Payload(ReceivePayloadErr(Box::new(e)))); + return Err(MeilisearchHttpError::Payload(ReceivePayload(Box::new(e)))); } if buffer_write_size == 0 { @@ -266,23 +262,21 @@ async fn document_addition( } if let Err(e) = buffer.seek(std::io::SeekFrom::Start(0)).await { - return Err(MeilisearchHttpError::Payload(ReceivePayloadErr(Box::new(e)))); - }; + return Err(MeilisearchHttpError::Payload(ReceivePayload(Box::new(e)))); + } let read_file = buffer.into_inner().into_std().await; - - let documents_count = - tokio::task::spawn_blocking(move || -> Result<_, MeilisearchHttpError> { - let documents_count = match format { - PayloadType::Json => read_json(&read_file, update_file.as_file_mut())?, - PayloadType::Csv => read_csv(&read_file, update_file.as_file_mut())?, - PayloadType::Ndjson => read_ndjson(&read_file, update_file.as_file_mut())?, - }; - // we NEED to persist the file here because we moved the `udpate_file` in another task. - update_file.persist()?; - Ok(documents_count) - }) - .await; + let documents_count = tokio::task::spawn_blocking(move || { + let documents_count = match format { + PayloadType::Json => read_json(&read_file, update_file.as_file_mut())?, + PayloadType::Csv => read_csv(&read_file, update_file.as_file_mut())?, + PayloadType::Ndjson => read_ndjson(&read_file, update_file.as_file_mut())?, + }; + // we NEED to persist the file here because we moved the `udpate_file` in another task. + update_file.persist()?; + Ok(documents_count) + }) + .await; let documents_count = match documents_count { Ok(Ok(documents_count)) => documents_count as u64,