1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +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 {
/// 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)
}
}

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 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,14 +219,22 @@ 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!")?;
self.curr_user_id = Some(user_id);
match account_helper::get_user_by_login_token(&token, self.api_client()) {
Ok(id) => {
self.curr_user_id = Some(id);
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
pub fn user_id(&self) -> ResultBoxError<UserID> {