mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Simplify code
This commit is contained in:
		@@ -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<dyn Error>) -> 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<String, Box<dyn Error>> {
 | 
			
		||||
    pub fn post_string(&mut self, name: &str) -> ResultBoxError<String> {
 | 
			
		||||
        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())
 | 
			
		||||
            },
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user