From 936b7123121acd129df9539ca6ab6ae43f686d5a Mon Sep 17 00:00:00 2001 From: forbesus Date: Tue, 1 Oct 2024 15:15:12 -0700 Subject: [PATCH] fix: add runOrLog function --- apps/api/src/routes/v1/dashboardData.ts | 86 ++++++------------- .../src/services/external/apiNodeService.ts | 1 - 2 files changed, 28 insertions(+), 59 deletions(-) diff --git a/apps/api/src/routes/v1/dashboardData.ts b/apps/api/src/routes/v1/dashboardData.ts index b286ac5de..0e1900311 100644 --- a/apps/api/src/routes/v1/dashboardData.ts +++ b/apps/api/src/routes/v1/dashboardData.ts @@ -149,66 +149,26 @@ const route = createRoute({ }); export default new OpenAPIHono().openapi(route, async c => { - - let chainStats; - let now; - let compare; - let networkCapacity; - let networkCapacityStats; - let latestBlocks; - let latestTransactions; - - try { - const chainStatsQuery = await getChainStats(); - chainStats = { ...chainStatsQuery }; - } catch (error) { - chainStats = {} - logger.error({ event: "Failed to fetch getChainStats", error }); - getSentry().captureException(error); - } - - try { - const dashboardData = await getDashboardData(); - now = dashboardData.now; - compare = dashboardData.compare; - } catch (error) { - logger.error({ event: "Failed to fetch dashboardData", error }); - getSentry().captureException(error); - } - - - try { - networkCapacity = await getNetworkCapacity(); - } catch (error) { - logger.error({ event: "Failed to fetch networkCapacity", error }); - getSentry().captureException(error); - } - - try { - networkCapacityStats = await getProviderGraphData("count"); - } catch (error) { - logger.error({ event: "Failed to fetch networkCapacityStats", error }); - getSentry().captureException(error); - } - - try { - latestBlocks = await getBlocks(5); - chainStats = { - ...chainStats, - height: latestBlocks[0]?.height ?? null, - transactionCount: latestBlocks[0]?.totalTransactionCount ?? null, + const chainStatsQuery = await runOrLog( + getChainStats, + { + bondedTokens: undefined, + totalSupply: undefined, + communityPool: undefined, + inflation: undefined, + stakingAPR: undefined } - } catch (error) { - logger.error({ event: "Failed to fetch latestBlocks", error }); - getSentry().captureException(error); - } - - try { - latestTransactions = await getTransactions(5); - } catch (error) { - logger.error({ event: "Failed to fetch latestTransactions", error }); - getSentry().captureException(error); + ); + const { now, compare } = await runOrLog(getDashboardData) + const networkCapacity = await runOrLog(getNetworkCapacity); + const networkCapacityStats = await runOrLog(() => getProviderGraphData("count")); + const latestBlocks = await runOrLog(() => getBlocks(5)); + const chainStats = { + ...chainStatsQuery, + height: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].height : undefined, + transactionCount: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].totalTransactionCount : undefined, } + const latestTransactions = await runOrLog(() => getTransactions(5)); return c.json({ chainStats, @@ -220,3 +180,13 @@ export default new OpenAPIHono().openapi(route, async c => { latestTransactions }); }); + +async function runOrLog(cb: () => Promise, defaultValue?: T): Promise { + try { + return await cb(); + } catch (error) { + logger.error({ event: `Failed to fetch ${cb.name}`, error }); + getSentry().captureException(error); + return defaultValue; + } +} \ No newline at end of file diff --git a/apps/api/src/services/external/apiNodeService.ts b/apps/api/src/services/external/apiNodeService.ts index 1e0982dcc..97a5ca811 100644 --- a/apps/api/src/services/external/apiNodeService.ts +++ b/apps/api/src/services/external/apiNodeService.ts @@ -36,7 +36,6 @@ export async function getChainStats() { 60 * 5, // 5 minutes cacheKeys.getChainStats, async () => { - // Define default values in case requests fail let bondedTokens; let totalSupply; let communityPool;