diff --git a/src/api_data/http_error.rs b/src/api_data/http_error.rs index 3e63f7a..b91d35b 100644 --- a/src/api_data/http_error.rs +++ b/src/api_data/http_error.rs @@ -15,43 +15,33 @@ pub struct HttpError { } impl HttpError { - /// Generate a 404 error - pub fn not_found(message: &str) -> HttpError { + /// Construct a new custom error + pub fn new(code: u16, message: &str) -> HttpError { HttpError { error: InnerHTTPError { - code: 404, + code, message: message.to_string(), } } } + /// Generate a 404 error + pub fn not_found(message: &str) -> HttpError { + HttpError::new(404, message) + } + /// Generate a 500 error pub fn internal_error(message: &str) -> HttpError { - HttpError { - error: InnerHTTPError { - code: 500, - message: message.to_string(), - } - } + HttpError::new(500, message) } /// Generate a 400 error pub fn bad_request(message: &str) -> HttpError { - HttpError { - error: InnerHTTPError { - code: 400, - message: message.to_string(), - } - } + HttpError::new(400, message) } /// Generate a 401 error pub fn forbidden(message: &str) -> HttpError { - HttpError { - error: InnerHTTPError { - code: 401, - message: message.to_string(), - } - } + HttpError::new(401, message) } } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 139be4f..0d857a2 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -1,16 +1,18 @@ -use actix_web::{web, HttpRequest, HttpResponse}; -use crate::controllers::routes::RequestResult; -use std::error::Error; -use serde::Serialize; -use crate::data::error::{ResultBoxError, ExecError}; use std::collections::HashMap; -use crate::helpers::{api_helper, account_helper}; -use actix_web::http::{HeaderName, HeaderValue}; +use std::error::Error; use std::str::FromStr; -use crate::data::config::conf; -use crate::data::api_client::APIClient; + +use actix_web::{HttpRequest, HttpResponse, web}; +use actix_web::http::{HeaderName, HeaderValue}; +use serde::Serialize; + use crate::api_data::http_error::HttpError; +use crate::controllers::routes::RequestResult; +use crate::data::api_client::APIClient; +use crate::data::config::conf; +use crate::data::error::{ExecError, ResultBoxError}; use crate::data::user::UserID; +use crate::helpers::{account_helper, api_helper}; /// Http request handler /// @@ -217,13 +219,21 @@ impl HttpRequestHandler { let token = self.post_string("userToken1")?; // Find user - let user_id = self.ok_or_bad_request( - account_helper::get_user_by_login_token(&token, self.api_client()), - "Please check your login tokens!")?; + match account_helper::get_user_by_login_token(&token, self.api_client()) { + Ok(id) => { + self.curr_user_id = Some(id); - self.curr_user_id = Some(user_id); - - Ok(()) + Ok(()) + } + Err(e) => { + println!("Error marking login tokens as invalid: {}", e); + self.response = Some( + actix_web::HttpResponse:: + build(actix_web::http::StatusCode::from_u16(412)?) + .json(HttpError::new(412, "Please check your login tokens!"))); + Err(e) + } + } } /// Get user ID. This function assess that a user ID is available to continue