add errors on content-type and add more serde debug

This commit is contained in:
qdequele 2020-05-07 12:29:18 +02:00
parent 7a8e64be30
commit cd12e2717c
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
2 changed files with 30 additions and 1 deletions

View File

@ -4,6 +4,7 @@ use actix_http::ResponseBuilder;
use actix_web as aweb;
use actix_web::http::StatusCode;
use serde_json::json;
use actix_web::error::JsonPayloadError;
#[derive(Debug)]
pub enum ResponseError {
@ -23,6 +24,8 @@ pub enum ResponseError {
FilterParsing(String),
RetrieveDocument(u64, String),
SearchDocuments(String),
PayloadTooLarge,
UnsupportedMediaType,
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::SearchDocuments(err) => write!(f, "impossible to search documents; {}", err),
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::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
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())
}
}
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()
}

View File

@ -8,6 +8,7 @@ pub mod option;
pub mod routes;
pub use self::data::Data;
use self::error::json_error_handler;
use actix_http::Error;
use actix_service::ServiceFactory;
use actix_web::{dev, web, App};
@ -28,7 +29,12 @@ pub fn create_app(
> {
App::new()
.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_css)
.configure(routes::document::services)