diff --git a/src/utils.spec.js b/src/utils.spec.js index 25c3bc9ef..32c88f8f3 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -147,19 +147,6 @@ describe('utils', () => { expect(_validate({})).rejects.toEqual(result.error); }); }); - - describe('when the fetch request is failing with not network error', () => { - test('rejects with the error', () => { - const result = new Error('Oh no'); - fetch.mockReturnValue({ - json: () => { - throw result; - } - }); - - expect(_validate({})).rejects.toEqual(result); - }); - }); }); describe('getScores', () => { const payload = { @@ -314,19 +301,6 @@ describe('utils', () => { expect(_getScores({})).rejects.toEqual(result.error); }); }); - - describe('when the fetch request is failing with not network error', () => { - test('rejects with the error', () => { - const result = new Error('Oh no'); - fetch.mockReturnValue({ - json: () => { - throw result; - } - }); - - expect(_getScores({})).rejects.toEqual(result); - }); - }); }); describe('getVp', () => { const payload = { @@ -458,18 +432,5 @@ describe('utils', () => { expect(_getVp({})).rejects.toEqual(result.error); }); }); - - describe('when the fetch request is failing with not network error', () => { - test('rejects with the error', () => { - const result = new Error('Oh no'); - fetch.mockReturnValue({ - json: () => { - throw result; - } - }); - - expect(_getVp({})).rejects.toEqual(result); - }); - }); }); }); diff --git a/src/utils.ts b/src/utils.ts index e4002df77..ba639fcb1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -134,7 +134,6 @@ ajv.addKeyword({ errors: true }); - // Custom URL format to allow empty string values // https://github.com/snapshot-labs/snapshot.js/pull/541/files ajv.addFormat('customUrl', { @@ -333,15 +332,27 @@ export async function getScores( headers: scoreApiHeaders, body: JSON.stringify({ params }) }); - const obj = await res.json(); + let json: Record = {}; + + try { + json = await res.json(); + } catch (e) { + if (!res.ok) { + return Promise.reject({ + code: res.status, + message: res.statusText, + data: '' + }); + } + } - if (obj.error) { - return Promise.reject(obj.error); + if (json.error) { + return Promise.reject(json.error); } return options.returnValue === 'all' - ? obj.result - : obj.result[options.returnValue || 'scores']; + ? json.result + : json.result[options.returnValue || 'scores']; } catch (e) { if (e.errno) { return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); @@ -401,7 +412,20 @@ export async function getVp( try { const res = await fetch(options.url, init); - const json = await res.json(); + let json: Record = {}; + + try { + json = await res.json(); + } catch (e) { + if (!res.ok) { + return Promise.reject({ + code: res.status, + message: res.statusText, + data: '' + }); + } + } + if (json.error) return Promise.reject(json.error); if (json.result) return json.result; } catch (e) { @@ -455,7 +479,20 @@ export async function validate( try { const res = await fetch(options.url, init); - const json = await res.json(); + let json: Record = {}; + + try { + json = await res.json(); + } catch (e) { + if (!res.ok) { + return Promise.reject({ + code: res.status, + message: res.statusText, + data: '' + }); + } + } + if (json.error) return Promise.reject(json.error); return json.result; } catch (e) { diff --git a/test/e2e/utils/getScores.spec.ts b/test/e2e/utils/getScores.spec.ts index b806edbf6..4d6680502 100644 --- a/test/e2e/utils/getScores.spec.ts +++ b/test/e2e/utils/getScores.spec.ts @@ -29,4 +29,22 @@ describe('test getScores', () => { data: '' }); }); + + test('getScores should return a promise rejection with JSON-RPC format on network error', async () => { + expect.assertions(1); + await expect( + getScores( + 'test.eth', + [], + '1', + ['0x9e8f6CF284Db7a80646D9d322A37b3dAF461F8DD'], + 'latest', + 'https://snapshot.org' + ) + ).to.rejects.toEqual({ + code: 405, + message: 'Method Not Allowed', + data: '' + }); + }); });