Change malformed_payload error

This commit is contained in:
many 2021-10-25 14:09:24 +02:00
parent 8ec0c4c913
commit 61c15b69fb
No known key found for this signature in database
GPG Key ID: 2CEF23B75189EACA
4 changed files with 49 additions and 34 deletions

View File

@ -39,9 +39,9 @@ impl fmt::Display for ErrorType {
use ErrorType::*; use ErrorType::*;
match self { match self {
InternalError => write!(f, "internal_error"), InternalError => write!(f, "internal"),
InvalidRequestError => write!(f, "invalid_request_error"), InvalidRequestError => write!(f, "invalid_request"),
AuthenticationError => write!(f, "authentication_error"), AuthenticationError => write!(f, "authentication"),
} }
} }
} }

View File

@ -42,8 +42,11 @@ pub struct ResponseError {
#[serde(skip)] #[serde(skip)]
code: StatusCode, code: StatusCode,
message: String, message: String,
#[serde(rename = "code")]
error_code: String, error_code: String,
#[serde(rename = "type")]
error_type: String, error_type: String,
#[serde(rename = "link")]
error_link: String, error_link: String,
} }

View File

@ -194,7 +194,7 @@ async fn error_add_malformed_csv_documents() {
let req = test::TestRequest::post() let req = test::TestRequest::post()
.uri("/indexes/dog/documents") .uri("/indexes/dog/documents")
.set_payload(document.to_string()) .set_payload(document.to_string())
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "text/csv"))
.to_request(); .to_request();
let res = test::call_service(&app, req).await; let res = test::call_service(&app, req).await;
let status_code = res.status(); let status_code = res.status();
@ -203,20 +203,22 @@ async fn error_add_malformed_csv_documents() {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!( assert_eq!(
response["message"], response["message"],
json!(r#"The csv payload provided is malformed. :syntaxErrorHelper."#) 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."#
)
); );
assert_eq!(response["code"], "malformed_payload"); assert_eq!(response["code"], json!("malformed_payload"));
assert_eq!(response["type"], "invalid_request"); assert_eq!(response["type"], json!("invalid_request"));
assert_eq!( assert_eq!(
response["link"], response["link"],
"https://docs.meilisearch.com/errors#malformed_payload" json!("https://docs.meilisearch.com/errors#malformed_payload")
); );
// put // put
let req = test::TestRequest::put() let req = test::TestRequest::put()
.uri("/indexes/dog/documents") .uri("/indexes/dog/documents")
.set_payload(document.to_string()) .set_payload(document.to_string())
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "text/csv"))
.to_request(); .to_request();
let res = test::call_service(&app, req).await; let res = test::call_service(&app, req).await;
let status_code = res.status(); let status_code = res.status();
@ -225,13 +227,15 @@ async fn error_add_malformed_csv_documents() {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!( assert_eq!(
response["message"], response["message"],
json!(r#"The csv payload provided is malformed. :syntaxErrorHelper."#) 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."#
)
); );
assert_eq!(response["code"], "malformed_payload"); assert_eq!(response["code"], json!("malformed_payload"));
assert_eq!(response["type"], "invalid_request"); assert_eq!(response["type"], json!("invalid_request"));
assert_eq!( assert_eq!(
response["link"], response["link"],
"https://docs.meilisearch.com/errors#malformed_payload" json!("https://docs.meilisearch.com/errors#malformed_payload")
); );
} }
@ -250,7 +254,7 @@ async fn error_add_malformed_json_documents() {
let req = test::TestRequest::post() let req = test::TestRequest::post()
.uri("/indexes/dog/documents") .uri("/indexes/dog/documents")
.set_payload(document.to_string()) .set_payload(document.to_string())
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "application/json"))
.to_request(); .to_request();
let res = test::call_service(&app, req).await; let res = test::call_service(&app, req).await;
let status_code = res.status(); let status_code = res.status();
@ -259,20 +263,22 @@ 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!(r#"The json payload provided is malformed. :syntaxErrorHelper."#) json!(
r#"The json payload provided is malformed. key must be a string at line 1 column 14."#
)
); );
assert_eq!(response["code"], "malformed_payload"); assert_eq!(response["code"], json!("malformed_payload"));
assert_eq!(response["type"], "invalid_request"); assert_eq!(response["type"], json!("invalid_request"));
assert_eq!( assert_eq!(
response["link"], response["link"],
"https://docs.meilisearch.com/errors#malformed_payload" json!("https://docs.meilisearch.com/errors#malformed_payload")
); );
// put // put
let req = test::TestRequest::put() let req = test::TestRequest::put()
.uri("/indexes/dog/documents") .uri("/indexes/dog/documents")
.set_payload(document.to_string()) .set_payload(document.to_string())
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "application/json"))
.to_request(); .to_request();
let res = test::call_service(&app, req).await; let res = test::call_service(&app, req).await;
let status_code = res.status(); let status_code = res.status();
@ -281,13 +287,15 @@ 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!(r#"The json payload provided is malformed. :syntaxErrorHelper."#) json!(
r#"The json payload provided is malformed. key must be a string at line 1 column 14."#
)
); );
assert_eq!(response["code"], "malformed_payload"); assert_eq!(response["code"], json!("malformed_payload"));
assert_eq!(response["type"], "invalid_request"); assert_eq!(response["type"], json!("invalid_request"));
assert_eq!( assert_eq!(
response["link"], response["link"],
"https://docs.meilisearch.com/errors#malformed_payload" json!("https://docs.meilisearch.com/errors#malformed_payload")
); );
} }
@ -306,7 +314,7 @@ async fn error_add_malformed_ndjson_documents() {
let req = test::TestRequest::post() let req = test::TestRequest::post()
.uri("/indexes/dog/documents") .uri("/indexes/dog/documents")
.set_payload(document.to_string()) .set_payload(document.to_string())
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "application/x-ndjson"))
.to_request(); .to_request();
let res = test::call_service(&app, req).await; let res = test::call_service(&app, req).await;
let status_code = res.status(); let status_code = res.status();
@ -315,20 +323,22 @@ async fn error_add_malformed_ndjson_documents() {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!( assert_eq!(
response["message"], response["message"],
json!(r#"The ndjson payload provided is malformed. :syntaxErrorHelper."#) json!(
r#"The ndjson payload provided is malformed. key must be a string at line 2 column 2."#
)
); );
assert_eq!(response["code"], "malformed_payload"); assert_eq!(response["code"], json!("malformed_payload"));
assert_eq!(response["type"], "invalid_request"); assert_eq!(response["type"], json!("invalid_request"));
assert_eq!( assert_eq!(
response["link"], response["link"],
"https://docs.meilisearch.com/errors#malformed_payload" json!("https://docs.meilisearch.com/errors#malformed_payload")
); );
// put // put
let req = test::TestRequest::put() let req = test::TestRequest::put()
.uri("/indexes/dog/documents") .uri("/indexes/dog/documents")
.set_payload(document.to_string()) .set_payload(document.to_string())
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "application/x-ndjson"))
.to_request(); .to_request();
let res = test::call_service(&app, req).await; let res = test::call_service(&app, req).await;
let status_code = res.status(); let status_code = res.status();
@ -337,13 +347,15 @@ async fn error_add_malformed_ndjson_documents() {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!( assert_eq!(
response["message"], response["message"],
json!(r#"The ndjson payload provided is malformed. :syntaxErrorHelper."#) json!(
r#"The ndjson payload provided is malformed. key must be a string at line 2 column 2."#
)
); );
assert_eq!(response["code"], "malformed_payload"); assert_eq!(response["code"], json!("malformed_payload"));
assert_eq!(response["type"], "invalid_request"); assert_eq!(response["type"], json!("invalid_request"));
assert_eq!( assert_eq!(
response["link"], response["link"],
"https://docs.meilisearch.com/errors#malformed_payload" json!("https://docs.meilisearch.com/errors#malformed_payload")
); );
} }

View File

@ -27,7 +27,7 @@ impl fmt::Display for PayloadType {
pub enum DocumentFormatError { pub enum DocumentFormatError {
#[error("Internal error: {0}")] #[error("Internal error: {0}")]
Internal(Box<dyn std::error::Error + Send + Sync + 'static>), Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
#[error("{0}. The {1} payload provided is malformed.")] #[error("The {1} payload provided is malformed. {0}.")]
MalformedPayload( MalformedPayload(
Box<dyn std::error::Error + Send + Sync + 'static>, Box<dyn std::error::Error + Send + Sync + 'static>,
PayloadType, PayloadType,