From 2bc5d26fa7ce6b94ebfbc4037c71831cf191789c Mon Sep 17 00:00:00 2001 From: Olli Vanhoja Date: Mon, 5 Sep 2016 05:13:20 +0300 Subject: [PATCH] Fix whois-ns-fallback request once again (#143) try-catch in getNameservers() doesn't work as expected because a promise is returned succesfully before it's evaluation and try-catch in the function won't catch if it fails later on. --- lib/index.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index c7b579d..93fd314 100644 --- a/lib/index.js +++ b/lib/index.js @@ -364,9 +364,11 @@ export default class Now extends EventEmitter { }); } - getNameservers (domain, { fallback = false } = {}) { - try { - return this.retry(async (bail, attempt) => { + getNameservers (domain) { + return new Promise((resolve, reject) => { + let fallback = false; + + this.retry(async (bail, attempt) => { if (this._debug) console.time(`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`); const res = await this._fetch(`/whois-ns${fallback ? '-fallback' : ''}?domain=${encodeURIComponent(domain)}`); if (this._debug) console.timeEnd(`> [debug] #${attempt} GET /whois-ns${fallback ? '-fallback' : ''}`); @@ -375,18 +377,17 @@ export default class Now extends EventEmitter { if ((!body.nameservers || body.nameservers.length === 0) && !fallback) { // if the nameservers are `null` it's likely // that our whois service failed to parse it - return this.getNameservers(domain, { fallback: true }); + fallback = true; + throw new Error('Invalid whois response'); } return body; } else { + if (attempt > 1) fallback = true; throw new Error(`Whois error (${res.status}): ${body.error.message}`); } - }); - } catch (err) { - if (fallback) throw err; - return this.getNameservers(domain, { fallback: true }); - } + }).then((body) => resolve(body)); + }); } // _ensures_ the domain is setup (idempotent)