[health api] better error handling

This commit is contained in:
nymkappa 2023-10-02 10:51:54 +02:00
parent 3c23e3ff84
commit 545b3e7325
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04

View File

@ -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<void> {
try { let response = {
// 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: { core: {
height: bitcoinCoreBlockHeight, height: -1
}, },
mempool: { mempool: {
height: mempoolBlockHeight, height: -1,
indexing: { indexing: {
indexedBlockCount: indexedBlockCount, enabled: Common.indexingEnabled(),
indexedBlockCountWithCPFP: indexedBlockWithCpfpCount, indexedBlockCount: -1,
indexedBlockCountWithCoinStats: indexedBlockWithCoinStatsCount, indexedBlockCountWithCPFP: -1,
} indexedBlockCountWithCoinStats: -1,
} }
},
}; };
// Bitcoin Core indexes try {
for (const indexName in bitcoinCoreIndexes) { // Bitcoin Core
let bitcoinCoreIndexes: number | string;
try {
bitcoinCoreIndexes = await bitcoinClient.getIndexInfo();
for (const indexName in bitcoinCoreIndexes as any) {
response.core[indexName.replace(/ /g,'_')] = bitcoinCoreIndexes[indexName]; 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') { if (config.MEMPOOL.BACKEND === 'esplora') {
const esploraBlockHeight = await bitcoinApi.$getBlockHeightTip(); try {
response['esplora'] = { response['esplora'] = {
height: esploraBlockHeight height: await bitcoinApi.$getBlockHeightTip()
}; };
} catch (e: any) {
response['esplora'] = {
height: -1,
error: e.message
};
}
} }
res.json(response); res.json(response);