From 58e2903177b02444a4e74c9018d7ca6823f38e9c Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 4 Mar 2022 10:46:59 +0800 Subject: [PATCH 01/16] first try --- meilisearch-lib/src/document_formats.rs | 28 ++++++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index cfd73038e..6b018e683 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -1,4 +1,5 @@ -use std::fmt; +use std::borrow::Borrow; +use std::fmt::{self, Debug, Display}; use std::io::{self, BufRead, BufReader, BufWriter, Cursor, Read, Seek, Write}; use meilisearch_error::{internal_error, Code, ErrorCode}; @@ -23,16 +24,27 @@ impl fmt::Display for PayloadType { } } -#[derive(thiserror::Error, Debug)] +#[derive(Debug)] pub enum DocumentFormatError { - #[error("An internal error has occurred. `{0}`.")] Internal(Box), - #[error("The `{1}` payload provided is malformed. `{0}`.")] - MalformedPayload( - Box, - PayloadType, - ), + MalformedPayload(Box, PayloadType), } +impl Display for DocumentFormatError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Internal(e) => write!(f, "An internal error has occurred. `{}`.", e), + Self::MalformedPayload(me, b) => match me.borrow() { + milli::documents::Error::JsonError(_) => write!( + f, + "The `{}` payload provided is malformed. line: {}", + b, "TODO" + ), + _ => write!(f, "The `{}` payload provided is malformed. line: {}", b, me), + }, + } + } +} +impl std::error::Error for DocumentFormatError {} impl From<(PayloadType, milli::documents::Error)> for DocumentFormatError { fn from((ty, error): (PayloadType, milli::documents::Error)) -> Self { From b138b92d393e6d41928253290789bf1d464f9e27 Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 4 Mar 2022 15:31:11 +0800 Subject: [PATCH 02/16] show detailed error message --- meilisearch-lib/src/document_formats.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index 6b018e683..b458a124e 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -34,12 +34,12 @@ impl Display for DocumentFormatError { match self { Self::Internal(e) => write!(f, "An internal error has occurred. `{}`.", e), Self::MalformedPayload(me, b) => match me.borrow() { - milli::documents::Error::JsonError(_) => write!( + milli::documents::Error::JsonError(se) => write!( f, - "The `{}` payload provided is malformed. line: {}", - b, "TODO" + "The `{}` payload provided is malformed. `Couldn't serialize document value. at line {} column {}`", + b,se.line(),se.column() ), - _ => write!(f, "The `{}` payload provided is malformed. line: {}", b, me), + _ => write!(f, "The `{}` payload provided is malformed. `{}`.", b, me), }, } } From a356c8359c67410f6ae0893201da595047d58f50 Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 4 Mar 2022 15:39:18 +0800 Subject: [PATCH 03/16] fix broken test --- meilisearch-http/tests/documents/add_documents.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index a8340096c..c3f8347f1 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -274,7 +274,7 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."# + r#"The `json` payload provided is malformed. `Couldn't serialize document value. at line 1 column 14`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -298,7 +298,7 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."# + r#"The `json` payload provided is malformed. `Couldn't serialize document value. at line 1 column 14`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -336,7 +336,7 @@ async fn error_add_malformed_ndjson_documents() { assert_eq!( response["message"], json!( - r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 2`."# + r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value. at line 1 column 2`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -360,7 +360,7 @@ async fn error_add_malformed_ndjson_documents() { assert_eq!( response["message"], json!( - r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 2`."# + r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value. at line 1 column 2`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); From c8895cab774a13efeec39edadbf9a0d3ec8f0019 Mon Sep 17 00:00:00 2001 From: LiuHanCheng <2463765697@qq.com> Date: Tue, 8 Mar 2022 12:03:59 +0800 Subject: [PATCH 04/16] Update meilisearch-lib/src/document_formats.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Renault --- meilisearch-lib/src/document_formats.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index b458a124e..b169cdf99 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -32,14 +32,14 @@ pub enum DocumentFormatError { impl Display for DocumentFormatError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Internal(e) => write!(f, "An internal error has occurred. `{}`.", e), + Self::Internal(e) => write!(f, "An internal error has occurred: `{}`.", e), Self::MalformedPayload(me, b) => match me.borrow() { milli::documents::Error::JsonError(se) => write!( f, - "The `{}` payload provided is malformed. `Couldn't serialize document value. at line {} column {}`", - b,se.line(),se.column() + "The `{}` payload provided is malformed. `Couldn't serialize document value at line {} column {}`", + b, se.line(), se.column() ), - _ => write!(f, "The `{}` payload provided is malformed. `{}`.", b, me), + _ => write!(f, "The `{}` payload provided is malformed: `{}`.", b, me), }, } } From 35bf7ee53825b46168076ada4021bc89f89a78f1 Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Tue, 8 Mar 2022 12:26:02 +0800 Subject: [PATCH 05/16] fix test --- meilisearch-http/tests/documents/add_documents.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index c3f8347f1..f61c2f3c6 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -212,7 +212,7 @@ async fn error_add_malformed_csv_documents() { assert_eq!( response["message"], json!( - r#"The `csv` payload provided is malformed. `CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields`."# + r#"The `csv` payload provided is malformed: `CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -236,7 +236,7 @@ async fn error_add_malformed_csv_documents() { assert_eq!( response["message"], json!( - r#"The `csv` payload provided is malformed. `CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields`."# + r#"The `csv` payload provided is malformed: `CSV error: record 1 (line: 2, byte: 12): found record with 3 fields, but the previous record has 2 fields`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -274,7 +274,7 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value. at line 1 column 14`"# + r#"The `json` payload provided is malformed. `Couldn't serialize document value at line 1 column 14`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -298,7 +298,7 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value. at line 1 column 14`"# + r#"The `json` payload provided is malformed. `Couldn't serialize document value at line 1 column 14`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -336,7 +336,7 @@ async fn error_add_malformed_ndjson_documents() { assert_eq!( response["message"], json!( - r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value. at line 1 column 2`"# + r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value at line 1 column 2`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -360,7 +360,7 @@ async fn error_add_malformed_ndjson_documents() { assert_eq!( response["message"], json!( - r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value. at line 1 column 2`"# + r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value at line 1 column 2`"# ) ); assert_eq!(response["code"], json!("malformed_payload")); From ce85981a4e3ee458296ca69b6859f52a91c603bf Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 25 Mar 2022 20:53:28 +0800 Subject: [PATCH 06/16] add truncate logic --- meilisearch-lib/src/document_formats.rs | 28 +++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index b169cdf99..409f56acf 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -34,11 +34,31 @@ impl Display for DocumentFormatError { match self { Self::Internal(e) => write!(f, "An internal error has occurred: `{}`.", e), Self::MalformedPayload(me, b) => match me.borrow() { - milli::documents::Error::JsonError(se) => write!( + milli::documents::Error::JsonError(se) => { + // "invalid type: {}, expected {}" + let mut serde_msg = se.to_string(); + + // https://github.com/meilisearch/meilisearch/issues/2107 + // The user input maybe insanely long. We need to truncate it. + let prefix = r#"invalid type: string ""#; + + if serde_msg.starts_with(prefix) { + let start_idx = prefix.len(); + if let Some(end_idx) = serde_msg.rfind("\"") { + if end_idx - start_idx > 100 { + serde_msg.replace_range(start_idx + 50..end_idx - 50, " ... "); + } + } else { + serde_msg = String::from(""); + } + } + + write!( f, - "The `{}` payload provided is malformed. `Couldn't serialize document value at line {} column {}`", - b, se.line(), se.column() - ), + "The `{}` payload provided is malformed. `Couldn't serialize document value: {}. at line {} column {} `", + b, se.line(), se.column(),serde_msg + ) + } _ => write!(f, "The `{}` payload provided is malformed: `{}`.", b, me), }, } From 3c72f4dc51a5a8a14818a02016fcff6e2013a73e Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 25 Mar 2022 21:31:23 +0800 Subject: [PATCH 07/16] fix test and add truncate test. --- .../tests/documents/add_documents.rs | 75 ++++++++++++++++++- meilisearch-lib/src/document_formats.rs | 4 +- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index 34ff24ac2..26a4858ba 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -1,5 +1,6 @@ use crate::common::{GetAllDocumentsOptions, Server}; use actix_web::test; +use itertools::Itertools; use meilisearch_http::{analytics, create_app}; use serde_json::{json, Value}; use time::{format_description::well_known::Rfc3339, OffsetDateTime}; @@ -274,7 +275,7 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value at line 1 column 14`"# + r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -298,7 +299,73 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value at line 1 column 14`"# + r#"The `json` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 14`."# + ) + ); + assert_eq!(response["code"], json!("malformed_payload")); + assert_eq!(response["type"], json!("invalid_request")); + assert_eq!( + response["link"], + json!("https://docs.meilisearch.com/errors#malformed_payload") + ); + + // truncate + + // length = 100 + let long = String::from_utf8( + "0123456789" + .as_bytes() + .iter() + .cycle() + .cloned() + .take(100) + .collect_vec(), + ) + .unwrap(); + + let document = format!("\"{}\"", long); + let req = test::TestRequest::put() + .uri("/indexes/dog/documents") + .set_payload(document) + .insert_header(("content-type", "application/json")) + .to_request(); + let res = test::call_service(&app, req).await; + let body = test::read_body(res).await; + dbg!(&body); + let response: Value = serde_json::from_slice(&body).unwrap_or_default(); + dbg!(&response); + assert_eq!(status_code, 400); + assert_eq!( + response["message"], + json!( + r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", expected a documents, or a sequence of documents. at line 1 column 102`."# + ) + ); + assert_eq!(response["code"], json!("malformed_payload")); + assert_eq!(response["type"], json!("invalid_request")); + assert_eq!( + response["link"], + json!("https://docs.meilisearch.com/errors#malformed_payload") + ); + + + + let document = format!("\"{}m\"", long); + let req = test::TestRequest::put() + .uri("/indexes/dog/documents") + .set_payload(document) + .insert_header(("content-type", "application/json")) + .to_request(); + let res = test::call_service(&app, req).await; + let body = test::read_body(res).await; + dbg!(&body); + let response: Value = serde_json::from_slice(&body).unwrap_or_default(); + dbg!(&response); + assert_eq!(status_code, 400); + assert_eq!( + response["message"], + json!( + r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "01234567890123456789012345678901234567890123456789 ... 1234567890123456789012345678901234567890123456789m", expected a documents, or a sequence of documents. at line 1 column 103`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -336,7 +403,7 @@ async fn error_add_malformed_ndjson_documents() { assert_eq!( response["message"], json!( - r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value at line 1 column 2`"# + r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 2`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -360,7 +427,7 @@ async fn error_add_malformed_ndjson_documents() { assert_eq!( response["message"], json!( - r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value at line 1 column 2`"# + r#"The `ndjson` payload provided is malformed. `Couldn't serialize document value: key must be a string at line 1 column 2`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index 409f56acf..c116039ff 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -55,8 +55,8 @@ impl Display for DocumentFormatError { write!( f, - "The `{}` payload provided is malformed. `Couldn't serialize document value: {}. at line {} column {} `", - b, se.line(), se.column(),serde_msg + "The `{}` payload provided is malformed. `Couldn't serialize document value: {}`.", + b,serde_msg ) } _ => write!(f, "The `{}` payload provided is malformed: `{}`.", b, me), From ce12000af31daf7863f8b41dd6cc8cf8b4c80fde Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 25 Mar 2022 21:33:47 +0800 Subject: [PATCH 08/16] add some comments --- meilisearch-http/tests/documents/add_documents.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index 26a4858ba..4afc801b8 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -348,8 +348,7 @@ async fn error_add_malformed_json_documents() { json!("https://docs.meilisearch.com/errors#malformed_payload") ); - - + // add one more char to the long string to test if the truncating works. let document = format!("\"{}m\"", long); let req = test::TestRequest::put() .uri("/indexes/dog/documents") From c7b489f8cb397140f77571293f6e9faeef2f6319 Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Fri, 25 Mar 2022 21:36:11 +0800 Subject: [PATCH 09/16] tidy --- meilisearch-lib/src/document_formats.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index c116039ff..bddb16680 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -35,13 +35,10 @@ impl Display for DocumentFormatError { Self::Internal(e) => write!(f, "An internal error has occurred: `{}`.", e), Self::MalformedPayload(me, b) => match me.borrow() { milli::documents::Error::JsonError(se) => { - // "invalid type: {}, expected {}" - let mut serde_msg = se.to_string(); - // https://github.com/meilisearch/meilisearch/issues/2107 // The user input maybe insanely long. We need to truncate it. + let mut serde_msg = se.to_string(); let prefix = r#"invalid type: string ""#; - if serde_msg.starts_with(prefix) { let start_idx = prefix.len(); if let Some(end_idx) = serde_msg.rfind("\"") { From 80d8ac40affb5d0dae6da070dbfe5c7654cfb387 Mon Sep 17 00:00:00 2001 From: LiuHanCheng <2463765697@qq.com> Date: Mon, 28 Mar 2022 14:57:51 +0800 Subject: [PATCH 10/16] Update meilisearch-lib/src/document_formats.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Renault --- meilisearch-lib/src/document_formats.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index bddb16680..0298c90c5 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -29,6 +29,7 @@ pub enum DocumentFormatError { Internal(Box), MalformedPayload(Box, PayloadType), } + impl Display for DocumentFormatError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { From 13a0e78d3f2c86dc8793e8c625d9a63179f0e0d9 Mon Sep 17 00:00:00 2001 From: LiuHanCheng <2463765697@qq.com> Date: Mon, 28 Mar 2022 14:58:00 +0800 Subject: [PATCH 11/16] Update meilisearch-lib/src/document_formats.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Renault --- meilisearch-lib/src/document_formats.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index 0298c90c5..424313c8c 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -62,6 +62,7 @@ impl Display for DocumentFormatError { } } } + impl std::error::Error for DocumentFormatError {} impl From<(PayloadType, milli::documents::Error)> for DocumentFormatError { From b28aa8e66600d57b4b51e51dfec8549dab605185 Mon Sep 17 00:00:00 2001 From: LiuHanCheng <2463765697@qq.com> Date: Thu, 31 Mar 2022 10:14:13 +0800 Subject: [PATCH 12/16] Update meilisearch-lib/src/document_formats.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Renault --- meilisearch-lib/src/document_formats.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index 424313c8c..27097a81b 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -39,22 +39,15 @@ impl Display for DocumentFormatError { // https://github.com/meilisearch/meilisearch/issues/2107 // The user input maybe insanely long. We need to truncate it. let mut serde_msg = se.to_string(); - let prefix = r#"invalid type: string ""#; - if serde_msg.starts_with(prefix) { - let start_idx = prefix.len(); - if let Some(end_idx) = serde_msg.rfind("\"") { - if end_idx - start_idx > 100 { - serde_msg.replace_range(start_idx + 50..end_idx - 50, " ... "); - } - } else { - serde_msg = String::from(""); - } + let ellipsis = "..."; + if serde_msg.len() > 100 + ellipsis.len() { + serde_msg.replace_range(50..serde_msg.len() - 50, ellipsis); } write!( - f, - "The `{}` payload provided is malformed. `Couldn't serialize document value: {}`.", - b,serde_msg + f, + "The `{}` payload provided is malformed. `Couldn't serialize document value: {}`.", + b, serde_msg ) } _ => write!(f, "The `{}` payload provided is malformed: `{}`.", b, me), From 403f03cb2ce202cb9c39b3e358c5f90ad46cfb4d Mon Sep 17 00:00:00 2001 From: LiuHanCheng <2463765697@qq.com> Date: Thu, 31 Mar 2022 10:14:22 +0800 Subject: [PATCH 13/16] Update meilisearch-http/tests/documents/add_documents.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Renault --- meilisearch-http/tests/documents/add_documents.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index 4afc801b8..5401112ab 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -312,16 +312,7 @@ async fn error_add_malformed_json_documents() { // truncate // length = 100 - let long = String::from_utf8( - "0123456789" - .as_bytes() - .iter() - .cycle() - .cloned() - .take(100) - .collect_vec(), - ) - .unwrap(); + let long = "0123456789".repeat(10); let document = format!("\"{}\"", long); let req = test::TestRequest::put() From 7ece7a9d9ee3015a84f3b19f6b7360b7099b5388 Mon Sep 17 00:00:00 2001 From: Liu Hancheng Date: Thu, 31 Mar 2022 10:39:21 +0800 Subject: [PATCH 14/16] change truncate strategy and coresponding test --- meilisearch-http/tests/documents/add_documents.rs | 5 ++--- meilisearch-lib/src/document_formats.rs | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index 5401112ab..1904f4979 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -1,6 +1,5 @@ use crate::common::{GetAllDocumentsOptions, Server}; use actix_web::test; -use itertools::Itertools; use meilisearch_http::{analytics, create_app}; use serde_json::{json, Value}; use time::{format_description::well_known::Rfc3339, OffsetDateTime}; @@ -329,7 +328,7 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", expected a documents, or a sequence of documents. at line 1 column 102`."# + r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "0123456789012345678901234567...890123456789", expected a documents, or a sequence of documents. at line 1 column 102`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); @@ -355,7 +354,7 @@ async fn error_add_malformed_json_documents() { assert_eq!( response["message"], json!( - r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "01234567890123456789012345678901234567890123456789 ... 1234567890123456789012345678901234567890123456789m", expected a documents, or a sequence of documents. at line 1 column 103`."# + r#"The `json` payload provided is malformed. `Couldn't serialize document value: invalid type: string "0123456789012345678901234567...90123456789m", expected a documents, or a sequence of documents. at line 1 column 103`."# ) ); assert_eq!(response["code"], json!("malformed_payload")); diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index 27097a81b..93c47afe8 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -41,7 +41,7 @@ impl Display for DocumentFormatError { let mut serde_msg = se.to_string(); let ellipsis = "..."; if serde_msg.len() > 100 + ellipsis.len() { - serde_msg.replace_range(50..serde_msg.len() - 50, ellipsis); + serde_msg.replace_range(50..serde_msg.len() - 85, ellipsis); } write!( From eee2cd5abfd7e26ac102b2a390324193822dc197 Mon Sep 17 00:00:00 2001 From: LiuHanCheng <2463765697@qq.com> Date: Fri, 1 Apr 2022 09:30:32 +0800 Subject: [PATCH 15/16] Update meilisearch-http/tests/documents/add_documents.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Renault --- meilisearch-http/tests/documents/add_documents.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index 1904f4979..682fd1f96 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -321,9 +321,7 @@ async fn error_add_malformed_json_documents() { .to_request(); let res = test::call_service(&app, req).await; let body = test::read_body(res).await; - dbg!(&body); let response: Value = serde_json::from_slice(&body).unwrap_or_default(); - dbg!(&response); assert_eq!(status_code, 400); assert_eq!( response["message"], From 6fc6b836327842235a9ebac2d4817197cebbec78 Mon Sep 17 00:00:00 2001 From: LiuHanCheng <2463765697@qq.com> Date: Fri, 1 Apr 2022 09:30:40 +0800 Subject: [PATCH 16/16] Update meilisearch-http/tests/documents/add_documents.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Renault --- meilisearch-http/tests/documents/add_documents.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index 682fd1f96..8b91997d4 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -345,9 +345,7 @@ async fn error_add_malformed_json_documents() { .to_request(); let res = test::call_service(&app, req).await; let body = test::read_body(res).await; - dbg!(&body); let response: Value = serde_json::from_slice(&body).unwrap_or_default(); - dbg!(&response); assert_eq!(status_code, 400); assert_eq!( response["message"],