From 10bde63575877d5d94349206ec6817b3d32fcd48 Mon Sep 17 00:00:00 2001 From: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Sat, 6 Dec 2025 21:41:47 -0700 Subject: [PATCH] Improve error handling in account metrics fetch Added checks for missing BACKEND_URL and improved error handling in getAccountNumber and getAllAccounts functions. Now returns 'N/A' on fetch errors or missing data, and sets state to 'N/A' in catch blocks to prevent crashes. --- src/pages/parts/admin/ConfigValuesPart.tsx | 72 ++++++++++++++-------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/src/pages/parts/admin/ConfigValuesPart.tsx b/src/pages/parts/admin/ConfigValuesPart.tsx index 74372361..261dec60 100644 --- a/src/pages/parts/admin/ConfigValuesPart.tsx +++ b/src/pages/parts/admin/ConfigValuesPart.tsx @@ -6,37 +6,59 @@ import { conf } from "@/setup/config"; import { BACKEND_URL } from "@/setup/constants"; async function getAccountNumber() { - const response = await fetch(`${BACKEND_URL}/metrics`); - const text = await response.text(); - - // Adjusted regex to match any hostname - const regex = - /mw_provider_hostname_count{hostname="https?:\/\/[^"}]+"} (\d+)/g; - let total = 0; - let match = regex.exec(text); // Initial assignment outside the loop - - while (match !== null) { - total += parseInt(match[1], 10); - match = regex.exec(text); // Update the assignment at the end of the loop body + if (!BACKEND_URL) { + return "N/A"; } - if (total > 0) { - return total.toString(); + try { + const response = await fetch(`${BACKEND_URL}/metrics`); + if (!response.ok) { + return "N/A"; + } + const text = await response.text(); + + // Adjusted regex to match any hostname + const regex = + /mw_provider_hostname_count{hostname="https?:\/\/[^"}]+"} (\d+)/g; + let total = 0; + let match = regex.exec(text); // Initial assignment outside the loop + + while (match !== null) { + total += parseInt(match[1], 10); + match = regex.exec(text); // Update the assignment at the end of the loop body + } + + if (total > 0) { + return total.toString(); + } + return "0"; + } catch (error) { + return "N/A"; } - throw new Error("ACCOUNT_NUMBER not found"); } async function getAllAccounts() { - const response = await fetch(`${BACKEND_URL}/metrics`); - const text = await response.text(); - - const regex = /mw_user_count{namespace="movie-web"} (\d+)/; - const match = text.match(regex); - - if (match) { - return match[1]; + if (!BACKEND_URL) { + return "N/A"; + } + + try { + const response = await fetch(`${BACKEND_URL}/metrics`); + if (!response.ok) { + return "N/A"; + } + const text = await response.text(); + + const regex = /mw_user_count{namespace="movie-web"} (\d+)/; + const match = text.match(regex); + + if (match) { + return match[1]; + } + return "0"; + } catch (error) { + return "N/A"; } - throw new Error("USER_COUNT not found"); } function ConfigValue(props: { name: string; children?: ReactNode }) { @@ -65,6 +87,7 @@ export function ConfigValuesPart() { }) .catch((error) => { console.error("Error fetching account number:", error); + setAccountNumber("N/A"); }); getAllAccounts() @@ -73,6 +96,7 @@ export function ConfigValuesPart() { }) .catch((error) => { console.error("Error fetching all accounts:", error); + setAllAccounts("N/A"); }); }, []);