pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Pas 10bde635 a6f78daa

+45 -21
+45 -21
src/pages/parts/admin/ConfigValuesPart.tsx
··· 6 6 import { BACKEND_URL } from "@/setup/constants"; 7 7 8 8 async function getAccountNumber() { 9 - const response = await fetch(`${BACKEND_URL}/metrics`); 10 - const text = await response.text(); 9 + if (!BACKEND_URL) { 10 + return "N/A"; 11 + } 12 + 13 + try { 14 + const response = await fetch(`${BACKEND_URL}/metrics`); 15 + if (!response.ok) { 16 + return "N/A"; 17 + } 18 + const text = await response.text(); 11 19 12 - // Adjusted regex to match any hostname 13 - const regex = 14 - /mw_provider_hostname_count{hostname="https?:\/\/[^"}]+"} (\d+)/g; 15 - let total = 0; 16 - let match = regex.exec(text); // Initial assignment outside the loop 20 + // Adjusted regex to match any hostname 21 + const regex = 22 + /mw_provider_hostname_count{hostname="https?:\/\/[^"}]+"} (\d+)/g; 23 + let total = 0; 24 + let match = regex.exec(text); // Initial assignment outside the loop 17 25 18 - while (match !== null) { 19 - total += parseInt(match[1], 10); 20 - match = regex.exec(text); // Update the assignment at the end of the loop body 21 - } 26 + while (match !== null) { 27 + total += parseInt(match[1], 10); 28 + match = regex.exec(text); // Update the assignment at the end of the loop body 29 + } 22 30 23 - if (total > 0) { 24 - return total.toString(); 31 + if (total > 0) { 32 + return total.toString(); 33 + } 34 + return "0"; 35 + } catch (error) { 36 + return "N/A"; 25 37 } 26 - throw new Error("ACCOUNT_NUMBER not found"); 27 38 } 28 39 29 40 async function getAllAccounts() { 30 - const response = await fetch(`${BACKEND_URL}/metrics`); 31 - const text = await response.text(); 41 + if (!BACKEND_URL) { 42 + return "N/A"; 43 + } 44 + 45 + try { 46 + const response = await fetch(`${BACKEND_URL}/metrics`); 47 + if (!response.ok) { 48 + return "N/A"; 49 + } 50 + const text = await response.text(); 32 51 33 - const regex = /mw_user_count{namespace="movie-web"} (\d+)/; 34 - const match = text.match(regex); 52 + const regex = /mw_user_count{namespace="movie-web"} (\d+)/; 53 + const match = text.match(regex); 35 54 36 - if (match) { 37 - return match[1]; 55 + if (match) { 56 + return match[1]; 57 + } 58 + return "0"; 59 + } catch (error) { 60 + return "N/A"; 38 61 } 39 - throw new Error("USER_COUNT not found"); 40 62 } 41 63 42 64 function ConfigValue(props: { name: string; children?: ReactNode }) { ··· 65 87 }) 66 88 .catch((error) => { 67 89 console.error("Error fetching account number:", error); 90 + setAccountNumber("N/A"); 68 91 }); 69 92 70 93 getAllAccounts() ··· 73 96 }) 74 97 .catch((error) => { 75 98 console.error("Error fetching all accounts:", error); 99 + setAllAccounts("N/A"); 76 100 }); 77 101 }, []); 78 102