Error handling for user profiles

rust
Elnu 1 year ago
parent 5aa4c4d203
commit 21f490c183

@ -11,4 +11,4 @@ mod submission;
pub use submission::*; pub use submission::*;
mod database; mod database;
pub use database::Database; pub use database::{Database, DatabaseError};

@ -1,12 +1,13 @@
use std::ops::Deref; 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 rocket_dyn_templates::{context, Template};
use crate::{ use crate::{
cookies::LANG_COOKIE, cookies::LANG_COOKIE,
i18n::DEFAULT as DEFAULT_LANG, i18n::DEFAULT as DEFAULT_LANG,
models::{Settings, SessionUser, Database}, models::{Settings, SessionUser, Database, DatabaseError},
utils::AcceptLanguage, utils::AcceptLanguage,
}; };
@ -17,11 +18,19 @@ pub async fn get_user(
settings: &State<Settings>, settings: &State<Settings>,
database: &State<Database>, database: &State<Database>,
accept_language: AcceptLanguage, accept_language: AcceptLanguage,
) -> Template { ) -> Result<Template, Status> {
Template::render( 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", "user",
context! { context! {
profile_user: database.get_user_by_name(&user).unwrap(), profile_user,
submissions: database.get_submissions_by_user_name(&user).unwrap(), submissions: database.get_submissions_by_user_name(&user).unwrap(),
settings: settings.deref(), settings: settings.deref(),
lang: cookies lang: cookies
@ -31,5 +40,5 @@ pub async fn get_user(
.unwrap_or_else(|| vec![DEFAULT_LANG.to_owned()]), .unwrap_or_else(|| vec![DEFAULT_LANG.to_owned()]),
user: SessionUser::get(cookies).await.unwrap(), user: SessionUser::get(cookies).await.unwrap(),
}, },
) ))
} }

@ -6,8 +6,8 @@ use rocket::response::Redirect;
pub fn login() -> Redirect { pub fn login() -> Redirect {
Redirect::to(format!( Redirect::to(format!(
// Switch from response_type=code to response_type=token from URL generator // 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", "https://discord.com/api/oauth2/authorize?client_id={}&redirect_uri={}success&response_type=token&scope=identify",
client_id = env::var("CLIENT_ID").unwrap(), env::var("CLIENT_ID").unwrap(),
redirect_uri = format!("{}success", env::var("DOMAIN").unwrap()), env::var("DOMAIN").unwrap(),
)) ))
} }

Loading…
Cancel
Save