Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return http error code and message when failing to connect to score-api #957

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
});
});
});
});
53 changes: 45 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -333,15 +332,27 @@ export async function getScores(
headers: scoreApiHeaders,
body: JSON.stringify({ params })
});
const obj = await res.json();
let json: Record<string, any> = {};

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: '' });
Expand Down Expand Up @@ -401,7 +412,20 @@ export async function getVp(

try {
const res = await fetch(options.url, init);
const json = await res.json();
let json: Record<string, any> = {};

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) {
Expand Down Expand Up @@ -455,7 +479,20 @@ export async function validate(

try {
const res = await fetch(options.url, init);
const json = await res.json();
let json: Record<string, any> = {};

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) {
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/utils/getScores.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
});
});
});
Loading