2819: Replace a meaningless serde message r=irevoire a=onyxcherry

# Pull Request

## What does this PR do?
Fixes #2680
<!-- Please link the issue you're trying to fix with this PR, if none then please create an issue first. -->

I've renamed the `serde_msg` variable to a `message` as _message_ does or does not include the serde error message --> is more generic.
## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Tomasz Wiśniewski <tomasz@wisniewski.app>
This commit is contained in:
bors[bot] 2022-10-03 17:58:00 +00:00 committed by GitHub
commit 8ad5c3043c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -372,7 +372,7 @@ async fn error_add_malformed_json_documents() {
assert_eq!( assert_eq!(
response["message"], response["message"],
json!( json!(
r#"The `json` payload provided is malformed. `Couldn't serialize document value: data did not match any variant of untagged enum Either`."# r#"The `json` payload provided is malformed. `Couldn't serialize document value: data are neither an object nor a list of objects`."#
) )
); );
assert_eq!(response["code"], json!("malformed_payload")); assert_eq!(response["code"], json!("malformed_payload"));
@ -395,7 +395,7 @@ async fn error_add_malformed_json_documents() {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!( assert_eq!(
response["message"], response["message"],
json!("The `json` payload provided is malformed. `Couldn't serialize document value: data did not match any variant of untagged enum Either`.") json!("The `json` payload provided is malformed. `Couldn't serialize document value: data are neither an object nor a list of objects`.")
); );
assert_eq!(response["code"], json!("malformed_payload")); assert_eq!(response["code"], json!("malformed_payload"));
assert_eq!(response["type"], json!("invalid_request")); assert_eq!(response["type"], json!("invalid_request"));

View File

@ -8,6 +8,7 @@ use meilisearch_types::internal_error;
use milli::documents::{DocumentsBatchBuilder, Error}; use milli::documents::{DocumentsBatchBuilder, Error};
use milli::Object; use milli::Object;
use serde::Deserialize; use serde::Deserialize;
use serde_json::error::Category;
type Result<T> = std::result::Result<T, DocumentFormatError>; type Result<T> = std::result::Result<T, DocumentFormatError>;
@ -40,18 +41,24 @@ impl Display for DocumentFormatError {
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() { Self::MalformedPayload(me, b) => match me.borrow() {
Error::Json(se) => { Error::Json(se) => {
let mut message = match se.classify() {
Category::Data => {
"data are neither an object nor a list of objects".to_string()
}
_ => se.to_string(),
};
// https://github.com/meilisearch/meilisearch/issues/2107 // https://github.com/meilisearch/meilisearch/issues/2107
// The user input maybe insanely long. We need to truncate it. // The user input maybe insanely long. We need to truncate it.
let mut serde_msg = se.to_string();
let ellipsis = "..."; let ellipsis = "...";
let trim_input_prefix_len = 50; let trim_input_prefix_len = 50;
let trim_input_suffix_len = 85; let trim_input_suffix_len = 85;
if serde_msg.len() if message.len()
> trim_input_prefix_len + trim_input_suffix_len + ellipsis.len() > trim_input_prefix_len + trim_input_suffix_len + ellipsis.len()
{ {
serde_msg.replace_range( message.replace_range(
trim_input_prefix_len..serde_msg.len() - trim_input_suffix_len, trim_input_prefix_len..message.len() - trim_input_suffix_len,
ellipsis, ellipsis,
); );
} }
@ -59,7 +66,7 @@ impl Display for DocumentFormatError {
write!( write!(
f, f,
"The `{}` payload provided is malformed. `Couldn't serialize document value: {}`.", "The `{}` payload provided is malformed. `Couldn't serialize document value: {}`.",
b, serde_msg b, message
) )
} }
_ => write!(f, "The `{}` payload provided is malformed: `{}`.", b, me), _ => write!(f, "The `{}` payload provided is malformed: `{}`.", b, me),