fix most content file error

This commit is contained in:
Irevoire 2022-10-22 13:59:21 +02:00 committed by Clément Renault
parent e107f1b282
commit 99144b1419
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
3 changed files with 26 additions and 7 deletions

View File

@ -1,6 +1,6 @@
use actix_web as aweb;
use aweb::error::{JsonPayloadError, QueryPayloadError};
use meilisearch_types::document_formats::DocumentFormatError;
use meilisearch_types::document_formats::{DocumentFormatError, PayloadType};
use meilisearch_types::error::{Code, ErrorCode, ResponseError};
use serde_json::Value;
use tokio::task::JoinError;
@ -19,6 +19,8 @@ pub enum MeilisearchHttpError {
DocumentNotFound(String),
#[error("Invalid syntax for the filter parameter: `expected {}, found: {1}`.", .0.join(", "))]
InvalidExpression(&'static [&'static str], Value),
#[error("A {0} payload is missing.")]
MissingPayload(PayloadType),
#[error("The provided payload reached the size limit.")]
PayloadTooLarge,
#[error(transparent)]
@ -43,6 +45,7 @@ impl ErrorCode for MeilisearchHttpError {
fn error_code(&self) -> Code {
match self {
MeilisearchHttpError::MissingContentType(_) => Code::MissingContentType,
MeilisearchHttpError::MissingPayload(_) => Code::MissingPayload,
MeilisearchHttpError::InvalidContentType(_, _) => Code::InvalidContentType,
MeilisearchHttpError::DocumentNotFound(_) => Code::DocumentNotFound,
MeilisearchHttpError::InvalidExpression(_, _) => Code::Filter,

View File

@ -1,4 +1,4 @@
use std::io::Cursor;
use std::io::{Cursor, ErrorKind};
use actix_web::http::header::CONTENT_TYPE;
use actix_web::web::Data;
@ -228,6 +228,9 @@ async fn document_addition(
while let Some(bytes) = body.next().await {
buffer.extend_from_slice(&bytes?);
}
if buffer.is_empty() {
return Err(MeilisearchHttpError::MissingPayload(format));
}
let reader = Cursor::new(buffer);
let documents_count =
@ -239,18 +242,30 @@ async fn document_addition(
};
// we NEED to persist the file here because we moved the `udpate_file` in another task.
update_file.persist()?;
println!("file has been persisted");
Ok(documents_count)
})
.await;
let documents_count = match documents_count {
Ok(Ok(documents_count)) => documents_count as u64,
Ok(Err(e)) => {
index_scheduler.delete_update_file(uuid)?;
return Err(e);
// in this case the file has not possibly be persisted.
Ok(Err(e)) => return Err(e),
Err(e) => {
// Here the file MAY have been persisted or not.
// We don't know thus we ignore the file not found error.
match index_scheduler.delete_update_file(uuid) {
Ok(()) => (),
Err(index_scheduler::Error::FileStore(file_store::Error::IoError(e)))
if e.kind() == ErrorKind::NotFound =>
{
()
}
Err(e) => {
index_scheduler.delete_update_file(uuid)?;
log::warn!("Unknown error happened while deleting a malformed update file with uuid {uuid}: {e}");
}
}
// We still want to return the original error to the end user.
return Err(e.into());
}
};

View File

@ -560,6 +560,7 @@ async fn error_add_missing_payload_ndjson_documents() {
let status_code = res.status();
let body = test::read_body(res).await;
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
dbg!(&response);
assert_eq!(status_code, 400);
assert_eq!(response["message"], json!(r#"A ndjson payload is missing."#));
assert_eq!(response["code"], json!("missing_payload"));