missing payload error

This commit is contained in:
mpostma 2021-09-30 11:29:27 +02:00 committed by Tamo
parent 18cb514073
commit 4eb3817b03
No known key found for this signature in database
GPG key ID: 20CD8020AFA88D69
6 changed files with 49 additions and 17 deletions

View file

@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
@ -75,6 +76,16 @@ pub enum DocumentAdditionFormat {
Ndjson,
}
impl fmt::Display for DocumentAdditionFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DocumentAdditionFormat::Json => write!(f, "json"),
DocumentAdditionFormat::Ndjson => write!(f, "ndjson"),
DocumentAdditionFormat::Csv => write!(f, "csv"),
}
}
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Stats {

View file

@ -5,7 +5,7 @@ use meilisearch_error::{Code, ErrorCode};
use crate::{
document_formats::DocumentFormatError,
index_controller::update_file_store::UpdateFileStoreError,
index_controller::{update_file_store::UpdateFileStoreError, DocumentAdditionFormat},
};
pub type Result<T> = std::result::Result<T, UpdateLoopError>;
@ -26,6 +26,8 @@ pub enum UpdateLoopError {
// TODO: The reference to actix has to go.
#[error("{0}")]
PayloadError(#[from] actix_web::error::PayloadError),
#[error("A {0} payload is missing.")]
MissingPayload(DocumentAdditionFormat),
}
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for UpdateLoopError
@ -63,6 +65,7 @@ impl ErrorCode for UpdateLoopError {
actix_web::error::PayloadError::Overflow => Code::PayloadTooLarge,
_ => Code::Internal,
},
Self::MissingPayload(_) => Code::MissingPayload,
}
}
}

View file

@ -3,7 +3,7 @@ mod message;
pub mod status;
pub mod store;
use std::io;
use std::io::{self, BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
@ -191,9 +191,15 @@ impl UpdateLoop {
method,
format,
} => {
let reader = StreamReader::new(payload);
let mut reader = BufReader::new(StreamReader::new(payload));
let (content_uuid, mut update_file) = self.update_file_store.new_update()?;
tokio::task::spawn_blocking(move || -> Result<_> {
// check if the payload is empty, and return an error
reader.fill_buf()?;
if reader.buffer().is_empty() {
return Err(UpdateLoopError::MissingPayload(format));
}
match format {
DocumentAdditionFormat::Json => read_json(reader, &mut *update_file)?,
DocumentAdditionFormat::Csv => read_csv(reader, &mut *update_file)?,