1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Fix returned error in case of bad login tokens

This commit is contained in:
Pierre HUBERT 2020-05-24 18:33:47 +02:00
parent 399900077f
commit 89d5eac02b
2 changed files with 36 additions and 36 deletions

View File

@ -15,43 +15,33 @@ pub struct HttpError {
} }
impl HttpError { impl HttpError {
/// Generate a 404 error /// Construct a new custom error
pub fn not_found(message: &str) -> HttpError { pub fn new(code: u16, message: &str) -> HttpError {
HttpError { HttpError {
error: InnerHTTPError { error: InnerHTTPError {
code: 404, code,
message: message.to_string(), message: message.to_string(),
} }
} }
} }
/// Generate a 404 error
pub fn not_found(message: &str) -> HttpError {
HttpError::new(404, message)
}
/// Generate a 500 error /// Generate a 500 error
pub fn internal_error(message: &str) -> HttpError { pub fn internal_error(message: &str) -> HttpError {
HttpError { HttpError::new(500, message)
error: InnerHTTPError {
code: 500,
message: message.to_string(),
}
}
} }
/// Generate a 400 error /// Generate a 400 error
pub fn bad_request(message: &str) -> HttpError { pub fn bad_request(message: &str) -> HttpError {
HttpError { HttpError::new(400, message)
error: InnerHTTPError {
code: 400,
message: message.to_string(),
}
}
} }
/// Generate a 401 error /// Generate a 401 error
pub fn forbidden(message: &str) -> HttpError { pub fn forbidden(message: &str) -> HttpError {
HttpError { HttpError::new(401, message)
error: InnerHTTPError {
code: 401,
message: message.to_string(),
}
}
} }
} }

View File

@ -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 std::collections::HashMap;
use crate::helpers::{api_helper, account_helper}; use std::error::Error;
use actix_web::http::{HeaderName, HeaderValue};
use std::str::FromStr; 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::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::data::user::UserID;
use crate::helpers::{account_helper, api_helper};
/// Http request handler /// Http request handler
/// ///
@ -217,13 +219,21 @@ impl HttpRequestHandler {
let token = self.post_string("userToken1")?; let token = self.post_string("userToken1")?;
// Find user // Find user
let user_id = self.ok_or_bad_request( match account_helper::get_user_by_login_token(&token, self.api_client()) {
account_helper::get_user_by_login_token(&token, self.api_client()), Ok(id) => {
"Please check your login tokens!")?; 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 /// Get user ID. This function assess that a user ID is available to continue