better error handling

This commit is contained in:
Mylloon 2023-04-19 16:57:16 +02:00
parent 2f0c0934ac
commit 57ed1dd8e1
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 53 additions and 24 deletions

View file

@ -14,10 +14,14 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
}
#[derive(Content)]
struct PortfolioTemplate {
bots_app: Vec<File>,
persos_app: Vec<File>,
univ_content: String,
struct PortfolioTemplate<'a> {
bots_app: Option<Vec<File>>,
bots_loc: Option<String>,
persos_app: Option<Vec<File>>,
persos_loc: Option<String>,
univ_content: Option<String>,
univ_loc: Option<String>,
err_msg: &'a str,
}
#[once(time = 60)]
@ -29,30 +33,45 @@ pub fn get_page(config: Config) -> String {
let bots_apps_loc = format!("{projects_dir}/bots");
let bots_apps = glob(&format!("{bots_apps_loc}/*{ext}"))
.unwrap()
.map(|e| read_md_file(&e.unwrap().to_string_lossy()))
.map(|e| read_md_file(&e.unwrap().to_string_lossy()).unwrap())
.collect::<Vec<File>>();
// Get perso apps
let perso_apps_loc = format!("{projects_dir}/perso");
let perso_apps = glob(&format!("{perso_apps_loc}/*{ext}"))
.unwrap()
.map(|e| read_md_file(&e.unwrap().to_string_lossy()))
.map(|e| read_md_file(&e.unwrap().to_string_lossy()).unwrap())
.collect::<Vec<File>>();
let univ_loc = format!("{projects_dir}/univ{ext}");
let (bots_app, bots_loc) = if bots_apps.is_empty() {
(None, Some(bots_apps_loc))
} else {
(Some(bots_apps), None)
};
let (persos_app, persos_loc) = if perso_apps.is_empty() {
(None, Some(perso_apps_loc))
} else {
(Some(perso_apps), None)
};
let (univ_content, univ_loc) = match read_md_file(&univ_loc) {
Some(data) => (Some(data.content), None),
_ => (None, Some(univ_loc)),
};
config.tmpl.render(
"portfolio.html",
PortfolioTemplate {
bots_app: if bots_apps.is_empty() {
vec![read_md_file(&bots_apps_loc)]
} else {
bots_apps
},
persos_app: if perso_apps.is_empty() {
vec![read_md_file(&perso_apps_loc)]
} else {
perso_apps
},
univ_content: read_md_file(&format!("{projects_dir}/univ{ext}")).content,
bots_app,
bots_loc,
persos_app,
persos_loc,
univ_content,
univ_loc,
err_msg: "is empty",
},
Infos {
page_title: Some("Portfolio".to_string()),

View file

@ -71,11 +71,12 @@ pub struct File {
pub content: String,
}
pub fn read_md_file(filename: &str) -> File {
pub fn read_md_file(filename: &str) -> Option<File> {
// Read markdown file
let text = fs::read_to_string(filename).unwrap_or(format!("{filename} is empty"));
read_md(&text)
match fs::read_to_string(filename) {
Ok(text) => Some(read_md(&text)),
_ => None,
}
}
pub fn read_md(raw_text: &str) -> File {

View file

@ -13,13 +13,20 @@
</h3>
{{/info}} {{/metadata}}
<div class="subcontent">{{&content}}</div>
{{/bots_app}} {{^bots_app}}
<p>{{bots_loc}} {{err_msg}}</p>
{{/bots_app}}
<br />
</div>
<h2 class="subtitle">
<a href="https://git.mylloon.fr/Paris8">Projet de l'université</a>
</h2>
<div class="subcontent">{{&univ_content}}</div>
<div class="subcontent">
{{&univ_content}} {{^univ_content}}
<p>{{univ_loc}} {{err_msg}}</p>
{{/univ_content}}
</div>
<h2 class="subtitle">Projets perso</h2>
<div class="subcontent">
{{#persos_app}} {{#metadata}} {{#info}}
@ -28,6 +35,8 @@
</h3>
{{/info}} {{/metadata}}
<div class="subcontent">{{&content}}</div>
{{/persos_app}} {{^persos_app}}
<p>{{persos_loc}} {{err_msg}}</p>
{{/persos_app}}
<br />
</div>