mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 14:54:27 +01:00
add errors on content-type and add more serde debug
This commit is contained in:
parent
7a8e64be30
commit
cd12e2717c
@ -4,6 +4,7 @@ use actix_http::ResponseBuilder;
|
|||||||
use actix_web as aweb;
|
use actix_web as aweb;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
use actix_web::error::JsonPayloadError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ResponseError {
|
pub enum ResponseError {
|
||||||
@ -23,6 +24,8 @@ pub enum ResponseError {
|
|||||||
FilterParsing(String),
|
FilterParsing(String),
|
||||||
RetrieveDocument(u64, String),
|
RetrieveDocument(u64, String),
|
||||||
SearchDocuments(String),
|
SearchDocuments(String),
|
||||||
|
PayloadTooLarge,
|
||||||
|
UnsupportedMediaType,
|
||||||
FacetExpression(String),
|
FacetExpression(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +111,8 @@ impl fmt::Display for ResponseError {
|
|||||||
Self::RetrieveDocument(id, err) => write!(f, "impossible to retrieve the document with id: {}; {}", id, err),
|
Self::RetrieveDocument(id, err) => write!(f, "impossible to retrieve the document with id: {}; {}", id, err),
|
||||||
Self::SearchDocuments(err) => write!(f, "impossible to search documents; {}", err),
|
Self::SearchDocuments(err) => write!(f, "impossible to search documents; {}", err),
|
||||||
Self::FacetExpression(e) => write!(f, "error parsing facet filter expression: {}", e),
|
Self::FacetExpression(e) => write!(f, "error parsing facet filter expression: {}", e),
|
||||||
|
Self::PayloadTooLarge => f.write_str("Payload to large"),
|
||||||
|
Self::UnsupportedMediaType => f.write_str("Unsupported media type")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -138,6 +143,8 @@ impl aweb::error::ResponseError for ResponseError {
|
|||||||
Self::MissingAuthorizationHeader => StatusCode::FORBIDDEN,
|
Self::MissingAuthorizationHeader => StatusCode::FORBIDDEN,
|
||||||
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Self::Maintenance => StatusCode::SERVICE_UNAVAILABLE,
|
Self::Maintenance => StatusCode::SERVICE_UNAVAILABLE,
|
||||||
|
Self::PayloadTooLarge => StatusCode::PAYLOAD_TOO_LARGE,
|
||||||
|
Self::UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -190,3 +197,19 @@ impl From<actix_http::Error> for ResponseError {
|
|||||||
ResponseError::Internal(err.to_string())
|
ResponseError::Internal(err.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<JsonPayloadError> for ResponseError {
|
||||||
|
fn from(err: JsonPayloadError) -> ResponseError {
|
||||||
|
match err {
|
||||||
|
JsonPayloadError::Deserialize(err) => ResponseError::BadRequest(format!("BAD_JSON: {}", err)),
|
||||||
|
JsonPayloadError::Overflow => ResponseError::PayloadTooLarge,
|
||||||
|
JsonPayloadError::ContentType => ResponseError::UnsupportedMediaType,
|
||||||
|
JsonPayloadError::Payload(err) => ResponseError::BadRequest(format!("Problem decoding request: {}", err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn json_error_handler(err: JsonPayloadError) -> ResponseError {
|
||||||
|
err.into()
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@ pub mod option;
|
|||||||
pub mod routes;
|
pub mod routes;
|
||||||
|
|
||||||
pub use self::data::Data;
|
pub use self::data::Data;
|
||||||
|
use self::error::json_error_handler;
|
||||||
use actix_http::Error;
|
use actix_http::Error;
|
||||||
use actix_service::ServiceFactory;
|
use actix_service::ServiceFactory;
|
||||||
use actix_web::{dev, web, App};
|
use actix_web::{dev, web, App};
|
||||||
@ -28,7 +29,12 @@ pub fn create_app(
|
|||||||
> {
|
> {
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(web::Data::new(data.clone()))
|
.app_data(web::Data::new(data.clone()))
|
||||||
.app_data(web::JsonConfig::default().limit(1024 * 1024 * 10)) // Json Limit of 10Mb
|
.app_data(
|
||||||
|
web::JsonConfig::default()
|
||||||
|
.limit(1024 * 1024 * 10)
|
||||||
|
.content_type(|_mime| true) // Accept all mime types
|
||||||
|
.error_handler(|err, _req| json_error_handler(err).into()),
|
||||||
|
)
|
||||||
.service(routes::load_html)
|
.service(routes::load_html)
|
||||||
.service(routes::load_css)
|
.service(routes::load_css)
|
||||||
.configure(routes::document::services)
|
.configure(routes::document::services)
|
||||||
|
Loading…
Reference in New Issue
Block a user