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 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<Project> {
pub async fn fetch_pr() -> Result<Vec<Project>, Error> {
// Result<GithubResponse, Error>
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")
.header(ACCEPT, "application/vnd.github.text-match+json")
.send()
.await
.unwrap()
.await?
.json::<GithubResponse>()
.await
.unwrap();
.await?;
let mut list = vec![];
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)]
struct PortfolioTemplate {
error: bool,
projects: Vec<Project>,
waiting: Vec<Project>,
closed: Vec<Project>,
@ -41,8 +42,8 @@ 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 data = match fetch_pr().await {
Ok(projects) => {
let mut data: Vec<Project> = Vec::new();
let mut map: HashMap<&str, Vec<Pull>> = HashMap::new();
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();
});
config.tmpl.render(
"contrib.html",
PortfolioTemplate {
error: false,
projects: data
.iter()
.cloned()
@ -99,7 +99,19 @@ pub async fn get_page(config: Config) -> std::string::String {
.cloned()
.filter(|p| !p.pulls_closed.is_empty())
.collect(),
}
}
Err(_) => PortfolioTemplate {
error: true,
projects: Vec::new(),
waiting: Vec::new(),
closed: Vec::new(),
},
};
config.tmpl.render(
"contrib.html",
data,
Infos {
page_title: Some("Mes contributions".to_string()),
page_desc: Some("Contributions à l'opensource par Anri".to_string()),

View file

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