From 99c44c8d1ddd2c4ba0337bf8d31b7ac70b20e9ba Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:07:26 +0700 Subject: [PATCH 1/3] fix: return http error code and message when failing to connect to score-api --- src/utils.spec.js | 13 ------- src/utils.ts | 59 ++++++++++++++++++++++++++------ test/e2e/utils/getScores.spec.ts | 18 ++++++++++ 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/utils.spec.js b/src/utils.spec.js index 25c3bc9ef..280644be8 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -314,19 +314,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 = { diff --git a/src/utils.ts b/src/utils.ts index e4002df77..71ccb9a5a 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,16 +332,28 @@ 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: any) { + 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']; - } catch (e) { + ? json.result + : json.result[options.returnValue || 'scores']; + } catch (e: any) { if (e.errno) { return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); } @@ -401,10 +412,23 @@ 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: any) { + 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) { + } catch (e: any) { if (e.errno) { return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); } @@ -455,10 +479,23 @@ 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: any) { + 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) { + } catch (e: any) { if (e.errno) { return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); } 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: '' + }); + }); }); From ff7d8822fdfd9a20d65b9f9413a3b04f8688115a Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:23:28 +0700 Subject: [PATCH 2/3] fix: fix tests --- src/utils.spec.js | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/utils.spec.js b/src/utils.spec.js index 280644be8..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 = { @@ -445,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); - }); - }); }); }); From b7b62bf0a7d1f7d7b8324f6807e1e6feffdc6c65 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:28:13 +0700 Subject: [PATCH 3/3] fix: fix invalid catch type annotation --- src/utils.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 71ccb9a5a..ba639fcb1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -336,7 +336,7 @@ export async function getScores( try { json = await res.json(); - } catch (e: any) { + } catch (e) { if (!res.ok) { return Promise.reject({ code: res.status, @@ -353,7 +353,7 @@ export async function getScores( return options.returnValue === 'all' ? json.result : json.result[options.returnValue || 'scores']; - } catch (e: any) { + } catch (e) { if (e.errno) { return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); } @@ -416,7 +416,7 @@ export async function getVp( try { json = await res.json(); - } catch (e: any) { + } catch (e) { if (!res.ok) { return Promise.reject({ code: res.status, @@ -428,7 +428,7 @@ export async function getVp( if (json.error) return Promise.reject(json.error); if (json.result) return json.result; - } catch (e: any) { + } catch (e) { if (e.errno) { return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); } @@ -483,7 +483,7 @@ export async function validate( try { json = await res.json(); - } catch (e: any) { + } catch (e) { if (!res.ok) { return Promise.reject({ code: res.status, @@ -495,7 +495,7 @@ export async function validate( if (json.error) return Promise.reject(json.error); return json.result; - } catch (e: any) { + } catch (e) { if (e.errno) { return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); }