From 545b3e7325418d04daf90aa7f605b8b0653a2d60 Mon Sep 17 00:00:00 2001 From: nymkappa <1612910616@pm.me> Date: Mon, 2 Oct 2023 10:51:54 +0200 Subject: [PATCH] [health api] better error handling --- backend/src/api/bitcoin/bitcoin.routes.ts | 78 ++++++++++++++--------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/backend/src/api/bitcoin/bitcoin.routes.ts b/backend/src/api/bitcoin/bitcoin.routes.ts index fec31c4a3..860d73cf0 100644 --- a/backend/src/api/bitcoin/bitcoin.routes.ts +++ b/backend/src/api/bitcoin/bitcoin.routes.ts @@ -128,44 +128,62 @@ class BitcoinRoutes { ; } - app.get(config.MEMPOOL.API_URL_PREFIX + 'health', this.generateHealthReport) + app.get(config.MEMPOOL.API_URL_PREFIX + 'health', this.generateHealthReport); } - private async generateHealthReport(req: Request, res: Response) { + private async generateHealthReport(req: Request, res: Response): Promise { + let response = { + core: { + height: -1 + }, + mempool: { + height: -1, + indexing: { + enabled: Common.indexingEnabled(), + indexedBlockCount: -1, + indexedBlockCountWithCPFP: -1, + indexedBlockCountWithCoinStats: -1, + } + }, + }; + try { // Bitcoin Core - const bitcoinCoreBlockHeight = await bitcoinCoreApi.$getBlockHeightTip(); - const bitcoinCoreIndexes = await bitcoinClient.getIndexInfo(); - // Mempool - const mempoolBlockHeight = blocks.getCurrentBlockHeight(); - const indexedBlockCount = await BlocksRepository.$getIndexedBlockCount(); - const indexedBlockWithCpfpCount = await BlocksRepository.$getIndexedCpfpBlockCount(); - const indexedBlockWithCoinStatsCount = await BlocksRepository.$getIndexedCoinStatsBlockCount(); - - const response = { - core: { - height: bitcoinCoreBlockHeight, - }, - mempool: { - height: mempoolBlockHeight, - indexing: { - indexedBlockCount: indexedBlockCount, - indexedBlockCountWithCPFP: indexedBlockWithCpfpCount, - indexedBlockCountWithCoinStats: indexedBlockWithCoinStatsCount, - } + let bitcoinCoreIndexes: number | string; + try { + bitcoinCoreIndexes = await bitcoinClient.getIndexInfo(); + for (const indexName in bitcoinCoreIndexes as any) { + response.core[indexName.replace(/ /g,'_')] = bitcoinCoreIndexes[indexName]; } - }; - - // Bitcoin Core indexes - for (const indexName in bitcoinCoreIndexes) { - response.core[indexName.replace(/ /g,'_')] = bitcoinCoreIndexes[indexName]; + } catch (e: any) { + response.core['error'] = e.message; + } + try { + response.core.height = await bitcoinCoreApi.$getBlockHeightTip(); + } catch (e: any) { + response.core['error'] = e.message; } + // Mempool + response.mempool.height = blocks.getCurrentBlockHeight(); + if (Common.indexingEnabled()) { + response.mempool.indexing.indexedBlockCount = await BlocksRepository.$getIndexedBlockCount(); + response.mempool.indexing.indexedBlockCountWithCPFP = await BlocksRepository.$getIndexedCpfpBlockCount(); + response.mempool.indexing.indexedBlockCountWithCoinStats = await BlocksRepository.$getIndexedCoinStatsBlockCount(); + } + + // Esplora if (config.MEMPOOL.BACKEND === 'esplora') { - const esploraBlockHeight = await bitcoinApi.$getBlockHeightTip(); - response['esplora'] = { - height: esploraBlockHeight - }; + try { + response['esplora'] = { + height: await bitcoinApi.$getBlockHeightTip() + }; + } catch (e: any) { + response['esplora'] = { + height: -1, + error: e.message + }; + } } res.json(response);