From b18f6f41906fad3322520fe3370f9327bb133f38 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 May 2020 10:19:15 +0200 Subject: [PATCH] Simplify code --- src/data/http_request_handler.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 646f685..2564f62 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -1,10 +1,9 @@ use actix_web::{web, HttpRequest, HttpResponse}; use crate::controllers::routes::RequestResult; use crate::data::http_error::HttpError; -use std::convert::TryFrom; use std::error::Error; use serde::Serialize; -use crate::data::error::{ResultBoxError}; +use crate::data::error::{ResultBoxError, ExecError}; use std::collections::HashMap; /// Http request handler @@ -73,14 +72,14 @@ impl HttpRequestHandler { pub fn internal_error(&mut self, error: Box) -> RequestResult { self.response = Some(HttpResponse::InternalServerError().json( HttpError::internal_error("Internal server error."))); - Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap()) + Err(error) } /// Bad request pub fn bad_request(&mut self, message: String) -> RequestResult { self.response = Some(HttpResponse::BadRequest().json( 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 @@ -110,12 +109,14 @@ impl HttpRequestHandler { } /// Get a post string - pub fn post_string(&mut self, name: &str) -> Result> { + pub fn post_string(&mut self, name: &str) -> ResultBoxError { let param = self.post_parameter(name)?; match ¶m.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()) + }, } } } \ No newline at end of file