1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-29 16:56:28 +00:00

Simplify code

This commit is contained in:
Pierre HUBERT 2020-05-23 10:19:15 +02:00
parent 0bd9a8f5f8
commit b18f6f4190

View File

@ -1,10 +1,9 @@
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use crate::controllers::routes::RequestResult; use crate::controllers::routes::RequestResult;
use crate::data::http_error::HttpError; use crate::data::http_error::HttpError;
use std::convert::TryFrom;
use std::error::Error; use std::error::Error;
use serde::Serialize; use serde::Serialize;
use crate::data::error::{ResultBoxError}; use crate::data::error::{ResultBoxError, ExecError};
use std::collections::HashMap; use std::collections::HashMap;
/// Http request handler /// Http request handler
@ -73,14 +72,14 @@ impl HttpRequestHandler {
pub fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult { pub fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult {
self.response = Some(HttpResponse::InternalServerError().json( self.response = Some(HttpResponse::InternalServerError().json(
HttpError::internal_error("Internal server error."))); HttpError::internal_error("Internal server error.")));
Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap()) Err(error)
} }
/// Bad request /// Bad request
pub fn bad_request(&mut self, message: String) -> RequestResult { pub fn bad_request(&mut self, message: String) -> RequestResult {
self.response = Some(HttpResponse::BadRequest().json( self.response = Some(HttpResponse::BadRequest().json(
HttpError::bad_request(&message))); HttpError::bad_request(&message)));
Err(Box::try_from(actix_web::error::ErrorBadRequest(message)).unwrap()) Err(Box::new(ExecError::new(&message)))
} }
/// Get the path of the request /// Get the path of the request
@ -110,12 +109,14 @@ impl HttpRequestHandler {
} }
/// Get a post string /// Get a post string
pub fn post_string(&mut self, name: &str) -> Result<String, Box<dyn Error>> { pub fn post_string(&mut self, name: &str) -> ResultBoxError<String> {
let param = self.post_parameter(name)?; let param = self.post_parameter(name)?;
match &param.string { match &param.string {
None => Err(Box::new(actix_web::error::ErrorBadRequest(format!("{} is not a string!", name)))), Some(s) => Ok(s.to_string()),
Some(s) => Ok(s.to_string()) None => {
Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err())
},
} }
} }
} }