From 0efa5350a5902564beac2de10cf1bdf76e42bb43 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Thu, 10 Dec 2020 23:22:46 +0100 Subject: [PATCH] Gracefully handle failed network requests --- src/serviceproviders.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/serviceproviders.js b/src/serviceproviders.js index da580ae..e6433a2 100644 --- a/src/serviceproviders.js +++ b/src/serviceproviders.js @@ -72,15 +72,31 @@ const directRequestHandler = (spData, opts) => { switch (spData.proof.format) { case 'json': - res = await req(url, null, { + req(url, null, { Accept: 'application/json', 'User-Agent': `doipjs/${require('../package.json').version}`, }) - resolve(await res.json()) + .then(async (res) => { + return await res.json() + }) + .then((res) => { + resolve(res) + }) + .catch((e) => { + reject(e) + }) break case 'text': - res = await req(url) - resolve(await res.text()) + req(url) + .then(async (res) => { + return await res.text() + }) + .then((res) => { + resolve(res) + }) + .catch((e) => { + reject(e) + }) break default: reject('No specified proof data format') @@ -92,13 +108,20 @@ const directRequestHandler = (spData, opts) => { const proxyRequestHandler = (spData, opts) => { return new Promise(async (resolve, reject) => { const url = spData.proof.fetch ? spData.proof.fetch : spData.proof.uri - const res = await req( + req( utils.generateProxyURL(spData.proof.format, url, opts), null, { Accept: 'application/json' } ) - const json = await res.json() - resolve(json.content) + .then(async (res) => { + return await res.json() + }) + .then((res) => { + resolve(res.content) + }) + .catch((e) => { + reject(e) + }) }) }