[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> {
let response = {
core: {
height: -1
},
mempool: {
height: -1,
indexing: {
enabled: Common.indexingEnabled(),
indexedBlockCount: -1,
indexedBlockCountWithCPFP: -1,
indexedBlockCountWithCoinStats: -1,
}
},
};
try { try {
// Bitcoin Core // Bitcoin Core
const bitcoinCoreBlockHeight = await bitcoinCoreApi.$getBlockHeightTip(); let bitcoinCoreIndexes: number | string;
const bitcoinCoreIndexes = await bitcoinClient.getIndexInfo(); try {
// Mempool bitcoinCoreIndexes = await bitcoinClient.getIndexInfo();
const mempoolBlockHeight = blocks.getCurrentBlockHeight(); for (const indexName in bitcoinCoreIndexes as any) {
const indexedBlockCount = await BlocksRepository.$getIndexedBlockCount(); response.core[indexName.replace(/ /g,'_')] = bitcoinCoreIndexes[indexName];
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,
}
} }
}; } catch (e: any) {
response.core['error'] = e.message;
// Bitcoin Core indexes }
for (const indexName in bitcoinCoreIndexes) { try {
response.core[indexName.replace(/ /g,'_')] = bitcoinCoreIndexes[indexName]; 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);