From 6d3f9e9f0182cc43b2cf087d377ca8b40f853de0 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 23 Jul 2021 09:38:45 -0400 Subject: [PATCH] Better handling of timeouts in pubToEpub() --- services/gunDB/contact-api/utils/index.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 08da945f..0a802879 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -180,7 +180,9 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { */ const pubToEpub = async pub => { try { - const epub = await timeout10( + const TIMEOUT_PTR = {} + + const epubOrTimeout = await Promise.race([ CommonUtils.makePromise(res => { require('../../Mediator/index') .getGun() @@ -191,14 +193,23 @@ const pubToEpub = async pub => { res(data) } }) + }), + CommonUtils.makePromise(res => { + setTimeout(() => { + res(TIMEOUT_PTR) + }, 10000) }) - ) + ]) - return epub + if (epubOrTimeout === TIMEOUT_PTR) { + throw new Error(`Timeout inside pubToEpub()`) + } + + return epubOrTimeout } catch (err) { logger.error(`Error inside pubToEpub:`) logger.error(err) - throw new Error(`pubToEpub() -> ${err.message}`) + throw err } }