better error handling
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-12 01:21:07 +02:00
parent cfe0620aa9
commit addf6e0035
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 74 additions and 61 deletions

View file

@ -1,6 +1,6 @@
use core::panic; use core::panic;
use reqwest::header::ACCEPT; use reqwest::{header::ACCEPT, Error};
use serde::Deserialize; use serde::Deserialize;
use crate::utils::get_reqwest_client; use crate::utils::get_reqwest_client;
@ -52,7 +52,7 @@ pub struct Project {
pub contrib_url: String, pub contrib_url: String,
} }
pub async fn fetch_pr() -> Vec<Project> { pub async fn fetch_pr() -> Result<Vec<Project>, Error> {
// Result<GithubResponse, Error> // Result<GithubResponse, Error>
let client = get_reqwest_client(); let client = get_reqwest_client();
@ -60,11 +60,9 @@ pub async fn fetch_pr() -> Vec<Project> {
.get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon") .get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon")
.header(ACCEPT, "application/vnd.github.text-match+json") .header(ACCEPT, "application/vnd.github.text-match+json")
.send() .send()
.await .await?
.unwrap()
.json::<GithubResponse>() .json::<GithubResponse>()
.await .await?;
.unwrap();
let mut list = vec![]; let mut list = vec![];
resp.items.iter().for_each(|p| { resp.items.iter().for_each(|p| {
@ -86,5 +84,5 @@ pub async fn fetch_pr() -> Vec<Project> {
}); });
}); });
list Ok(list)
} }

View file

@ -16,6 +16,7 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
#[derive(Content)] #[derive(Content)]
struct PortfolioTemplate { struct PortfolioTemplate {
error: bool,
projects: Vec<Project>, projects: Vec<Project>,
waiting: Vec<Project>, waiting: Vec<Project>,
closed: Vec<Project>, closed: Vec<Project>,
@ -41,8 +42,8 @@ struct Pull {
#[once(time = 120)] #[once(time = 120)]
pub async fn get_page(config: Config) -> std::string::String { pub async fn get_page(config: Config) -> std::string::String {
// Fetch latest data from github // Fetch latest data from github
let projects = fetch_pr().await; let data = match fetch_pr().await {
Ok(projects) => {
let mut data: Vec<Project> = Vec::new(); let mut data: Vec<Project> = Vec::new();
let mut map: HashMap<&str, Vec<Pull>> = HashMap::new(); let mut map: HashMap<&str, Vec<Pull>> = HashMap::new();
projects.iter().for_each(|p| { projects.iter().for_each(|p| {
@ -81,9 +82,8 @@ pub async fn get_page(config: Config) -> std::string::String {
d.name = name.into_iter().collect(); d.name = name.into_iter().collect();
}); });
config.tmpl.render(
"contrib.html",
PortfolioTemplate { PortfolioTemplate {
error: false,
projects: data projects: data
.iter() .iter()
.cloned() .cloned()
@ -99,7 +99,19 @@ pub async fn get_page(config: Config) -> std::string::String {
.cloned() .cloned()
.filter(|p| !p.pulls_closed.is_empty()) .filter(|p| !p.pulls_closed.is_empty())
.collect(), .collect(),
}
}
Err(_) => PortfolioTemplate {
error: true,
projects: Vec::new(),
waiting: Vec::new(),
closed: Vec::new(),
}, },
};
config.tmpl.render(
"contrib.html",
data,
Infos { Infos {
page_title: Some("Mes contributions".to_string()), page_title: Some("Mes contributions".to_string()),
page_desc: Some("Contributions à l'opensource par Anri".to_string()), page_desc: Some("Contributions à l'opensource par Anri".to_string()),

View file

@ -4,6 +4,9 @@
<body> <body>
{{#data}} {{#data}}
<div id="content"> <div id="content">
{{#error}}
<p>Github ne veut pas que tu vois ces informations...</p>
{{/error}} {{^error}}
<h1 class="subtitle">Mes contributions</h1> <h1 class="subtitle">Mes contributions</h1>
<!-- Add URL? --> <!-- Add URL? -->
{{#projects}} {{#projects}}
@ -31,6 +34,6 @@
</p> </p>
{{/closed}} {{/closed}}
</div> </div>
{{/data}} {{> footer.html }} {{/error}} {{/data}} {{> footer.html }}
</body> </body>
</html> </html>