From 21f490c183534d0cfd83517800435e55963f114d Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Tue, 4 Jul 2023 11:49:29 -0700 Subject: [PATCH] Error handling for user profiles --- src/models/mod.rs | 2 +- src/routes/get_user.rs | 21 +++++++++++++++------ src/routes/login.rs | 6 +++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/models/mod.rs b/src/models/mod.rs index 470aeb2..a2aa09a 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -11,4 +11,4 @@ mod submission; pub use submission::*; mod database; -pub use database::Database; \ No newline at end of file +pub use database::{Database, DatabaseError}; \ No newline at end of file diff --git a/src/routes/get_user.rs b/src/routes/get_user.rs index 64883d4..6bad5a4 100644 --- a/src/routes/get_user.rs +++ b/src/routes/get_user.rs @@ -1,12 +1,13 @@ use std::ops::Deref; -use rocket::{http::CookieJar, State}; +use reqwest::StatusCode; +use rocket::{http::{CookieJar, Status}, State, Response}; use rocket_dyn_templates::{context, Template}; use crate::{ cookies::LANG_COOKIE, i18n::DEFAULT as DEFAULT_LANG, - models::{Settings, SessionUser, Database}, + models::{Settings, SessionUser, Database, DatabaseError}, utils::AcceptLanguage, }; @@ -17,11 +18,19 @@ pub async fn get_user( settings: &State, database: &State, accept_language: AcceptLanguage, -) -> Template { - Template::render( +) -> Result { + let profile_user = match database.get_user_by_name(&user) { + Ok(profile_user) => profile_user, + Err(DatabaseError::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) => return Err(Status::NotFound), + Err(error) => { + eprintln!("{:?}", error); + return Err(Status::InternalServerError); + }, + }; + Ok(Template::render( "user", context! { - profile_user: database.get_user_by_name(&user).unwrap(), + profile_user, submissions: database.get_submissions_by_user_name(&user).unwrap(), settings: settings.deref(), lang: cookies @@ -31,5 +40,5 @@ pub async fn get_user( .unwrap_or_else(|| vec![DEFAULT_LANG.to_owned()]), user: SessionUser::get(cookies).await.unwrap(), }, - ) + )) } \ No newline at end of file diff --git a/src/routes/login.rs b/src/routes/login.rs index b0de556..5ea5ea3 100644 --- a/src/routes/login.rs +++ b/src/routes/login.rs @@ -6,8 +6,8 @@ use rocket::response::Redirect; pub fn login() -> Redirect { Redirect::to(format!( // Switch from response_type=code to response_type=token from URL generator - "https://discord.com/api/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=token&scope=identify", - client_id = env::var("CLIENT_ID").unwrap(), - redirect_uri = format!("{}success", env::var("DOMAIN").unwrap()), + "https://discord.com/api/oauth2/authorize?client_id={}&redirect_uri={}success&response_type=token&scope=identify", + env::var("CLIENT_ID").unwrap(), + env::var("DOMAIN").unwrap(), )) }