Convert JSON payload deserialization errors into validation errors

This commit is contained in:
silverpill 2022-10-02 17:44:08 +00:00
parent 93ebdfadd4
commit 54c8f241e4
3 changed files with 24 additions and 1 deletions

18
src/http.rs Normal file
View file

@ -0,0 +1,18 @@
use actix_web::{
HttpRequest,
error::{Error, JsonPayloadError},
};
use crate::errors::HttpError;
pub fn json_error_handler(
error: JsonPayloadError,
_: &HttpRequest,
) -> Error {
match error {
JsonPayloadError::Deserialize(de_error) => {
HttpError::ValidationError(de_error.to_string()).into()
},
other_error => other_error.into(),
}
}

View file

@ -6,6 +6,7 @@ pub mod database;
mod errors; mod errors;
pub mod ethereum; pub mod ethereum;
mod frontend; mod frontend;
pub mod http;
mod http_signatures; mod http_signatures;
mod ipfs; mod ipfs;
pub mod logger; pub mod logger;

View file

@ -13,6 +13,7 @@ use mitra::config::{parse_config, Environment};
use mitra::database::{get_database_client, create_pool}; use mitra::database::{get_database_client, create_pool};
use mitra::database::migrate::apply_migrations; use mitra::database::migrate::apply_migrations;
use mitra::ethereum::contracts::get_contracts; use mitra::ethereum::contracts::get_contracts;
use mitra::http::json_error_handler;
use mitra::logger::configure_logger; use mitra::logger::configure_logger;
use mitra::mastodon_api::accounts::views::account_api_scope; use mitra::mastodon_api::accounts::views::account_api_scope;
use mitra::mastodon_api::directory::views::directory_api_scope; use mitra::mastodon_api::directory::views::directory_api_scope;
@ -123,7 +124,10 @@ async fn main() -> std::io::Result<()> {
}) })
.wrap(create_auth_error_handler()) .wrap(create_auth_error_handler())
.app_data(web::PayloadConfig::default().limit(UPLOAD_MAX_SIZE * 2)) .app_data(web::PayloadConfig::default().limit(UPLOAD_MAX_SIZE * 2))
.app_data(web::JsonConfig::default().limit(UPLOAD_MAX_SIZE * 2)) .app_data(web::JsonConfig::default()
.limit(UPLOAD_MAX_SIZE * 2)
.error_handler(json_error_handler)
)
.app_data(web::Data::new(config.clone())) .app_data(web::Data::new(config.clone()))
.app_data(web::Data::new(db_pool.clone())) .app_data(web::Data::new(db_pool.clone()))
.app_data(web::Data::new(maybe_contract_set.clone())) .app_data(web::Data::new(maybe_contract_set.clone()))