fix(404 error): enhance the logging in the item loading process to diagnose the 404 error

This commit is contained in:
ryan 2025-02-24 17:42:07 +03:00
parent 03ffeb10fc
commit c9b24faad7

View file

@ -19,24 +19,47 @@ struct WikidataSuggestion {
//function to load items from database //function to load items from database
pub async fn load_items_from_db(current_url: &str) -> Result<Vec<Item>, String> { pub async fn load_items_from_db(current_url: &str) -> Result<Vec<Item>, String> {
//logging for the raw URL
log!("[DEBUG] Loading items for URL: {}", current_url);
let encoded_url = encode(&current_url); let encoded_url = encode(&current_url);
let api_url = format!("/api/urls/{}/items", encoded_url); let api_url = format!("/api/urls/{}/items", encoded_url);
// Log the constructed API URL
log!("[DEBUG] Making request to API endpoint: {}", api_url);
let response = gloo_net::http::Request::get(&api_url) let response = gloo_net::http::Request::get(&api_url)
.send() .send()
.await .await
.map_err(|err| format!("Failed to fetch items: {:?}", err))?; .map_err(|err| {
log!("[ERROR] Network error: {:?}", err);
if response.status() == 200 { format!("Failed to fetch items: {:?}", err)
// Deserialize into Vec<Item> })?;
log!("Loading items from DB..."); // Log response metadata
let items = response log!("[DEBUG] Received response - Status: {}", response.status());
.json::<Vec<Item>>() if response.status() == 200 {
.await log!("[DEBUG] Successfully received items");
.map_err(|err| format!("Failed to parse items: {:?}", err))?; let items = response
.json::<Vec<Item>>()
Ok(items) .await
.map_err(|err| {
log!("[ERROR] JSON parsing error: {:?}", err);
format!("Failed to parse items: {:?}", err)
})?;
log!("[DEBUG] Successfully parsed {} items", items.len());
Ok(items)
} else { } else {
let body = response.text().await.unwrap_or_default(); let body = response.text().await.unwrap_or_default();
log!("[ERROR] Server error details:
Status: {}
URL: {}
Response Body: {}
Request URL: {}",
response.status(),
api_url,
body,
current_url
);
Err(format!("Server error ({}): {}", response.status(), body)) Err(format!("Server error ({}): {}", response.status(), body))
} }
} }