fix: better error managment
This commit is contained in:
parent
9cef4843f0
commit
36b1b6c223
1 changed files with 38 additions and 16 deletions
|
|
@ -14,6 +14,20 @@ use serde_json::{Value, json};
|
||||||
// Ok(contents)
|
// Ok(contents)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
fn return_error() -> Json<Value> {
|
||||||
|
Json(json!(
|
||||||
|
{
|
||||||
|
"title": "Error",
|
||||||
|
"cards": [
|
||||||
|
{
|
||||||
|
"title": "An error occured",
|
||||||
|
"content": "Try reloading the page, or going back to the main page. Sorry !"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
// ====================
|
// ====================
|
||||||
// =====< ROUTES >=====
|
// =====< ROUTES >=====
|
||||||
// ====================
|
// ====================
|
||||||
|
|
@ -21,43 +35,51 @@ use serde_json::{Value, json};
|
||||||
pub async fn get_home(Path(lang): Path<String>) -> Json<Value> {
|
pub async fn get_home(Path(lang): Path<String>) -> Json<Value> {
|
||||||
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
||||||
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/home.json")).await {
|
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/home.json")).await {
|
||||||
Json(response.json().await.unwrap())
|
if let Ok(result) = response.json().await {
|
||||||
|
Json(result)
|
||||||
|
} else {
|
||||||
|
return_error()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Json(
|
return_error()
|
||||||
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_projects(Path(lang): Path<String>) -> Json<Value> {
|
pub async fn get_projects(Path(lang): Path<String>) -> Json<Value> {
|
||||||
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
||||||
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/projects.json")).await {
|
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/projects.json")).await {
|
||||||
Json(response.json().await.unwrap())
|
if let Ok(result) = response.json().await {
|
||||||
|
Json(result)
|
||||||
|
} else {
|
||||||
|
return_error()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Json(
|
return_error()
|
||||||
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_lessons(Path(lang): Path<String>) -> Json<Value> {
|
pub async fn get_lessons(Path(lang): Path<String>) -> Json<Value> {
|
||||||
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
||||||
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/lessons.json")).await {
|
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/lessons.json")).await {
|
||||||
Json(response.json().await.unwrap())
|
if let Ok(result) = response.json().await {
|
||||||
|
Json(result)
|
||||||
|
} else {
|
||||||
|
return_error()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Json(
|
return_error()
|
||||||
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_lessons_by_id(Path((lang, id)): Path<(String, String)>) -> Json<Value> {
|
pub async fn get_lessons_by_id(Path((lang, id)): Path<(String, String)>) -> Json<Value> {
|
||||||
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
|
||||||
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/lessons/{id}.json")).await {
|
if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/lessons/{id}.json")).await {
|
||||||
Json(response.json().await.unwrap())
|
if let Ok(result) = response.json().await {
|
||||||
|
Json(result)
|
||||||
|
} else {
|
||||||
|
return_error()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Json(
|
return_error()
|
||||||
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue