From addf6e00355e57a03c0876ae9b8daf9cb6e3791d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 12 Apr 2023 01:21:07 +0200 Subject: [PATCH] better error handling --- src/misc/github.rs | 12 ++--- src/routes/contrib.rs | 118 +++++++++++++++++++++++------------------ templates/contrib.html | 5 +- 3 files changed, 74 insertions(+), 61 deletions(-) diff --git a/src/misc/github.rs b/src/misc/github.rs index a155d75..d7a95c7 100644 --- a/src/misc/github.rs +++ b/src/misc/github.rs @@ -1,6 +1,6 @@ use core::panic; -use reqwest::header::ACCEPT; +use reqwest::{header::ACCEPT, Error}; use serde::Deserialize; use crate::utils::get_reqwest_client; @@ -52,7 +52,7 @@ pub struct Project { pub contrib_url: String, } -pub async fn fetch_pr() -> Vec { +pub async fn fetch_pr() -> Result, Error> { // Result let client = get_reqwest_client(); @@ -60,11 +60,9 @@ pub async fn fetch_pr() -> Vec { .get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon") .header(ACCEPT, "application/vnd.github.text-match+json") .send() - .await - .unwrap() + .await? .json::() - .await - .unwrap(); + .await?; let mut list = vec![]; resp.items.iter().for_each(|p| { @@ -86,5 +84,5 @@ pub async fn fetch_pr() -> Vec { }); }); - list + Ok(list) } diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index 14489f3..8470e70 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -16,6 +16,7 @@ pub async fn page(config: web::Data) -> impl Responder { #[derive(Content)] struct PortfolioTemplate { + error: bool, projects: Vec, waiting: Vec, closed: Vec, @@ -41,65 +42,76 @@ struct Pull { #[once(time = 120)] pub async fn get_page(config: Config) -> std::string::String { // Fetch latest data from github - let projects = fetch_pr().await; - - let mut data: Vec = Vec::new(); - let mut map: HashMap<&str, Vec> = HashMap::new(); - projects.iter().for_each(|p| { - let project = Pull { - url: p.contrib_url.clone(), - id: p.id, - name: p.project.clone(), - state: p.status as u8, - }; - let project_name = p.project.as_str(); - if map.contains_key(project_name) { - map.entry(project_name).and_modify(|v| v.push(project)); - } else { - data.push(Project { - name: project_name.to_string(), - url: p.project_url.clone(), - pulls_merged: Vec::new(), - pulls_closed: Vec::new(), - pulls_open: Vec::new(), + let data = match fetch_pr().await { + Ok(projects) => { + let mut data: Vec = Vec::new(); + let mut map: HashMap<&str, Vec> = HashMap::new(); + projects.iter().for_each(|p| { + let project = Pull { + url: p.contrib_url.clone(), + id: p.id, + name: p.project.clone(), + state: p.status as u8, + }; + let project_name = p.project.as_str(); + if map.contains_key(project_name) { + map.entry(project_name).and_modify(|v| v.push(project)); + } else { + data.push(Project { + name: project_name.to_string(), + url: p.project_url.clone(), + pulls_merged: Vec::new(), + pulls_closed: Vec::new(), + pulls_open: Vec::new(), + }); + map.insert(project_name, vec![project]); + } }); - map.insert(project_name, vec![project]); - } - }); - data.iter_mut().for_each(|d| { - map.get(d.name.as_str()).unwrap().iter().for_each(|p| { - let state = p.state.into(); - match state { - ProjectState::Closed => d.pulls_closed.push(p.clone()), - ProjectState::Merged => d.pulls_merged.push(p.clone()), - ProjectState::Open => d.pulls_open.push(p.clone()), + data.iter_mut().for_each(|d| { + map.get(d.name.as_str()).unwrap().iter().for_each(|p| { + let state = p.state.into(); + match state { + ProjectState::Closed => d.pulls_closed.push(p.clone()), + ProjectState::Merged => d.pulls_merged.push(p.clone()), + ProjectState::Open => d.pulls_open.push(p.clone()), + } + }); + let mut name: Vec = d.name.replace('-', " ").chars().collect(); + name[0] = name[0].to_uppercase().next().unwrap(); + d.name = name.into_iter().collect(); + }); + + PortfolioTemplate { + error: false, + projects: data + .iter() + .cloned() + .filter(|p| !p.pulls_merged.is_empty()) + .collect(), + waiting: data + .iter() + .cloned() + .filter(|p| !p.pulls_open.is_empty()) + .collect(), + closed: data + .iter() + .cloned() + .filter(|p| !p.pulls_closed.is_empty()) + .collect(), } - }); - let mut name: Vec = d.name.replace('-', " ").chars().collect(); - name[0] = name[0].to_uppercase().next().unwrap(); - d.name = name.into_iter().collect(); - }); + } + Err(_) => PortfolioTemplate { + error: true, + projects: Vec::new(), + waiting: Vec::new(), + closed: Vec::new(), + }, + }; config.tmpl.render( "contrib.html", - PortfolioTemplate { - projects: data - .iter() - .cloned() - .filter(|p| !p.pulls_merged.is_empty()) - .collect(), - waiting: data - .iter() - .cloned() - .filter(|p| !p.pulls_open.is_empty()) - .collect(), - closed: data - .iter() - .cloned() - .filter(|p| !p.pulls_closed.is_empty()) - .collect(), - }, + data, Infos { page_title: Some("Mes contributions".to_string()), page_desc: Some("Contributions à l'opensource par Anri".to_string()), diff --git a/templates/contrib.html b/templates/contrib.html index 766e75b..7206729 100644 --- a/templates/contrib.html +++ b/templates/contrib.html @@ -4,6 +4,9 @@ {{#data}}
+ {{#error}} +

Github ne veut pas que tu vois ces informations...

+ {{/error}} {{^error}}

Mes contributions

{{#projects}} @@ -31,6 +34,6 @@

{{/closed}}
- {{/data}} {{> footer.html }} + {{/error}} {{/data}} {{> footer.html }}