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