From dc82b92b1e0094c8d3a9dda54f8fe54b08d54a41 Mon Sep 17 00:00:00 2001 From: Uewotm90 <61500734+Uewotm90@users.noreply.github.com> Date: Thu, 7 Dec 2023 11:48:39 +0100 Subject: [PATCH] removed unwrap --- src/api/auth.rs | 7 ++++--- src/api/ecdar_api.rs | 42 +++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/api/auth.rs b/src/api/auth.rs index 70c3b40..9577762 100644 --- a/src/api/auth.rs +++ b/src/api/auth.rs @@ -14,7 +14,7 @@ use tonic::{ pub fn validation_interceptor(mut req: Request<()>) -> Result, Status> { let token = match req .token_string() - .map_err(|err| Status::internal("failed to get token string"))? + .map_err(|_err| Status::internal("failed to get token string"))? { Some(token) => Token::from_str(TokenType::AccessToken, &token), None => return Err(Status::unauthenticated("Token not found")), @@ -57,6 +57,7 @@ impl TokenType { /// /// # Panics /// This method will panic if the token secret environment variable is not set. + #[allow(clippy::expect_used)] fn secret(&self) -> String { match self { TokenType::AccessToken => env::var("ACCESS_TOKEN_HS512_SECRET") @@ -104,7 +105,7 @@ impl Token { let now = Utc::now(); let expiration = now .checked_add_signed(token_type.duration()) - .ok_or_else(|| TokenError::InvalidSignature)? + .ok_or(TokenError::InvalidSignature)? // .expect("valid timestamp") .timestamp(); @@ -301,7 +302,7 @@ impl RequestExt for Request { Err(_) => return None, }; //TODO better error handling - Some(uid.parse().ok()?) + uid.parse().ok() } } diff --git a/src/api/ecdar_api.rs b/src/api/ecdar_api.rs index 10c96ec..30c70fc 100644 --- a/src/api/ecdar_api.rs +++ b/src/api/ecdar_api.rs @@ -19,7 +19,7 @@ use crate::database::{session_context::SessionContextTrait, user_context::UserCo use crate::entities::{access, in_use, model, query, session, user}; use chrono::{Duration, Utc}; use regex::Regex; -use sea_orm::{DbErr, SqlErr}; +use sea_orm::SqlErr; use serde_json; use std::sync::Arc; use tonic::{Code, Request, Response, Status}; @@ -96,7 +96,7 @@ pub async fn handle_session( TokenType::RefreshToken, request .token_string() - .map_err(|err| Status::internal("failed to get token from request metadata"))? + .map_err(|_err| Status::internal("failed to get token from request metadata"))? .ok_or(Status::internal( "failed to get token from request metadata", ))?, @@ -203,7 +203,7 @@ impl EcdarApi for ConcreteEcdarApi { TokenType::AccessToken, request .token_string() - .map_err(|err| { + .map_err(|_err| { Status::internal( "failed to get token from request metadata", ) @@ -248,20 +248,24 @@ impl EcdarApi for ConcreteEcdarApi { let queries = queries .into_iter() - .map(|query| Query { - id: query.id, - model_id: query.model_id, - query: query.string, - result: match query.result { - Some(result) => { - serde_json::from_value(result).expect("failed to parse message") - //TODO better error handling - } - None => "".to_owned(), - }, - outdated: query.outdated, + .map(|query| { + let result = serde_json::from_value(query.result.unwrap_or_else(|| "".into()))?; + + Ok(Query { + id: query.id, + model_id: query.model_id, + query: query.string, + result: result, + outdated: query.outdated, + }) }) - .collect::>(); + .collect::, serde_json::Error>>() + .map_err(|err| { + Status::internal(format!( + "failed to parse json result, inner error: {}", + err.to_string() + )) + })?; Ok(Response::new(GetModelResponse { model: Some(model), @@ -331,7 +335,7 @@ impl EcdarApi for ConcreteEcdarApi { TokenType::AccessToken, request .token_string() - .map_err(|err| Status::internal("failed to get token from request metadata"))? + .map_err(|_err| Status::internal("failed to get token from request metadata"))? .ok_or(Status::internal( "Failed to get token from request metadata", ))?, @@ -416,7 +420,7 @@ impl EcdarApi for ConcreteEcdarApi { TokenType::AccessToken, request .token_string() - .map_err(|err| Status::internal("failed to get token from request metadata"))? + .map_err(|_err| Status::internal("failed to get token from request metadata"))? .ok_or(Status::new( Code::Internal, "Failed to get token from request metadata", @@ -864,7 +868,7 @@ impl EcdarApiAuth for ConcreteEcdarApi { TokenType::RefreshToken, request .token_str() - .map_err(|err| Status::internal("failed to get token from request metadata"))? + .map_err(|_err| Status::internal("failed to get token from request metadata"))? .ok_or(Status::unauthenticated("No refresh token provided"))?, ); let token_data = refresh_token.validate()?;