mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Better message
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
use core::fmt;
 | 
			
		||||
use serde::export::Formatter;
 | 
			
		||||
use std::error;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
 | 
			
		||||
/// Simple rust error
 | 
			
		||||
///
 | 
			
		||||
@@ -9,6 +10,7 @@ use std::error;
 | 
			
		||||
 | 
			
		||||
/// Simple result type
 | 
			
		||||
pub type ResultExecError<E> = Result<E, ExecError>;
 | 
			
		||||
pub type ResultBoxError<E> = Result<E, Box<dyn Error>>;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone)]
 | 
			
		||||
pub struct ExecError(pub String);
 | 
			
		||||
 
 | 
			
		||||
@@ -27,4 +27,12 @@ impl HttpError {
 | 
			
		||||
            message: message.to_string()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Generate a 401 error
 | 
			
		||||
    pub fn bad_request(message: &str) -> HttpError {
 | 
			
		||||
        HttpError {
 | 
			
		||||
            code: 401,
 | 
			
		||||
            message: message.to_string()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,7 @@ use crate::data::http_error::HttpError;
 | 
			
		||||
use std::convert::TryFrom;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
use crate::data::error::{ResultExecError, ExecError};
 | 
			
		||||
use crate::data::error::{ResultBoxError};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
/// Http request handler
 | 
			
		||||
@@ -76,14 +76,20 @@ impl HttpRequestHandler {
 | 
			
		||||
        Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// 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())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get the path of the request
 | 
			
		||||
    pub fn request_path(&self) -> String {
 | 
			
		||||
        self.request.path().to_string()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Check login tokens
 | 
			
		||||
    pub fn check_client_token(&mut self) -> ResultExecError<()> {
 | 
			
		||||
 | 
			
		||||
    pub fn check_client_token(&mut self) -> Result<(), Box<dyn Error>> {
 | 
			
		||||
        println!("me = {}", self.post_string("me")?);
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
@@ -95,17 +101,20 @@ impl HttpRequestHandler {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get a post parameter
 | 
			
		||||
    pub fn post_parameter(&self, name: &str) -> Result<&RequestValue, ExecError> {
 | 
			
		||||
        self.body.get(name)
 | 
			
		||||
            .ok_or(ExecError(format!("POST parameter {} not found in request!", name)))
 | 
			
		||||
    pub fn post_parameter(&mut self, name: &str) -> ResultBoxError<&RequestValue> {
 | 
			
		||||
        if !self.has_post_parameter(name) {
 | 
			
		||||
            self.bad_request(format!("POST parameter '{}' not found in request!", name))?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(self.body.get(name).unwrap())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get a post string
 | 
			
		||||
    pub fn post_string(&self, name: &str) -> Result<String, ExecError> {
 | 
			
		||||
    pub fn post_string(&mut self, name: &str) -> Result<String, Box<dyn Error>> {
 | 
			
		||||
        let param = self.post_parameter(name)?;
 | 
			
		||||
 | 
			
		||||
        match ¶m.string {
 | 
			
		||||
            None => Err(ExecError(format!("{} is not a string!", name))),
 | 
			
		||||
            None => Err(Box::new(actix_web::error::ErrorBadRequest(format!("{} is not a string!", name)))),
 | 
			
		||||
            Some(s) => Ok(s.to_string())
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user