Skip to content

Commit f15df41

Browse files
authored
fix: catch cache controller db errors (#1368)
1 parent c1dd9f1 commit f15df41

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/api/controllers/cache-controller.ts

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -249,41 +249,58 @@ async function calculateETag(
249249
): Promise<ETag | undefined> {
250250
switch (etagType) {
251251
case ETagType.chainTip:
252-
const chainTip = await db.getUnanchoredChainTip();
253-
if (!chainTip.found) {
254-
// This should never happen unless the API is serving requests before it has synced any blocks.
252+
try {
253+
const chainTip = await db.getUnanchoredChainTip();
254+
if (!chainTip.found) {
255+
// This should never happen unless the API is serving requests before it has synced any
256+
// blocks.
257+
return;
258+
}
259+
return chainTip.result.microblockHash ?? chainTip.result.indexBlockHash;
260+
} catch (error) {
261+
logger.error(`Unable to calculate chain_tip ETag: ${error}`);
255262
return;
256263
}
257-
return chainTip.result.microblockHash ?? chainTip.result.indexBlockHash;
258264

259265
case ETagType.mempool:
260-
const digest = await db.getMempoolTxDigest();
261-
if (!digest.found) {
262-
// This should never happen unless the API is serving requests before it has synced any blocks.
266+
try {
267+
const digest = await db.getMempoolTxDigest();
268+
if (!digest.found) {
269+
// This should never happen unless the API is serving requests before it has synced any
270+
// blocks.
271+
return;
272+
}
273+
if (digest.result.digest === null) {
274+
// A `null` mempool digest means the `bit_xor` postgres function is unavailable.
275+
return ETAG_EMPTY;
276+
}
277+
return digest.result.digest;
278+
} catch (error) {
279+
logger.error(`Unable to calculate mempool ETag: ${error}`);
263280
return;
264281
}
265-
if (digest.result.digest === null) {
266-
// A `null` mempool digest means the `bit_xor` postgres function is unavailable.
267-
return ETAG_EMPTY;
268-
}
269-
return digest.result.digest;
270282

271283
case ETagType.transaction:
272-
const { tx_id } = req.params;
273-
const normalizedTxId = normalizeHashString(tx_id);
274-
if (normalizedTxId === false) {
275-
return ETAG_EMPTY;
276-
}
277-
const status = await db.getTxStatus(normalizedTxId);
278-
if (!status.found) {
279-
return ETAG_EMPTY;
284+
try {
285+
const { tx_id } = req.params;
286+
const normalizedTxId = normalizeHashString(tx_id);
287+
if (normalizedTxId === false) {
288+
return ETAG_EMPTY;
289+
}
290+
const status = await db.getTxStatus(normalizedTxId);
291+
if (!status.found) {
292+
return ETAG_EMPTY;
293+
}
294+
const elements: string[] = [
295+
normalizedTxId,
296+
status.result.index_block_hash ?? '',
297+
status.result.microblock_hash ?? '',
298+
status.result.status.toString(),
299+
];
300+
return elements.join(':');
301+
} catch (error) {
302+
logger.error(`Unable to calculate transaction ETag: ${error}`);
303+
return;
280304
}
281-
const elements: string[] = [
282-
normalizedTxId,
283-
status.result.index_block_hash ?? '',
284-
status.result.microblock_hash ?? '',
285-
status.result.status.toString(),
286-
];
287-
return elements.join(':');
288305
}
289306
}

0 commit comments

Comments
 (0)