Error handling for user profiles

rust
Elnu 1 year ago
parent 5aa4c4d203
commit 21f490c183

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

@ -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<Settings>,
database: &State<Database>,
accept_language: AcceptLanguage,
) -> Template {
Template::render(
) -> Result<Template, Status> {
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(),
},
)
))
}

@ -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(),
))
}

Loading…
Cancel
Save