Skip to content

Commit 561e9ab

Browse files
authored
fix: return the same json error on network error (#900)
* fix: handle JSON-RPC errors from score-api * fix: return the same json error on network error * chore: remove only test
1 parent 3daf54a commit 561e9ab

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/utils.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ export async function getScores(
247247
? obj.result
248248
: obj.result[options.returnValue || 'scores'];
249249
} catch (e) {
250+
if (e.errno) {
251+
return Promise.reject({ code: e.errno, message: e.toString(), data: '' });
252+
}
250253
return Promise.reject(e);
251254
}
252255
}
@@ -278,10 +281,18 @@ export async function getVp(
278281
}
279282
})
280283
};
281-
const res = await fetch(options.url, init);
282-
const json = await res.json();
283-
if (json.error) return Promise.reject(json.error);
284-
if (json.result) return json.result;
284+
285+
try {
286+
const res = await fetch(options.url, init);
287+
const json = await res.json();
288+
if (json.error) return Promise.reject(json.error);
289+
if (json.result) return json.result;
290+
} catch (e) {
291+
if (e.errno) {
292+
return Promise.reject({ code: e.errno, message: e.toString(), data: '' });
293+
}
294+
return Promise.reject(e);
295+
}
285296
}
286297

287298
export async function validate(
@@ -311,10 +322,18 @@ export async function validate(
311322
}
312323
})
313324
};
314-
const res = await fetch(options.url, init);
315-
const json = await res.json();
316-
if (json.error) return Promise.reject(json.error);
317-
return json.result;
325+
326+
try {
327+
const res = await fetch(options.url, init);
328+
const json = await res.json();
329+
if (json.error) return Promise.reject(json.error);
330+
return json.result;
331+
} catch (e) {
332+
if (e.errno) {
333+
return Promise.reject({ code: e.errno, message: e.toString(), data: '' });
334+
}
335+
return Promise.reject(e);
336+
}
318337
}
319338

320339
export function validateSchema(schema, data) {
@@ -417,8 +436,7 @@ export async function getDelegatesBySpace(
417436
snapshot = 'latest',
418437
options: any = {}
419438
) {
420-
const subgraphUrl =
421-
options.subgraphUrl || SNAPSHOT_SUBGRAPH_URL[network];
439+
const subgraphUrl = options.subgraphUrl || SNAPSHOT_SUBGRAPH_URL[network];
422440
if (!subgraphUrl) {
423441
return Promise.reject(
424442
`Delegation subgraph not available for network ${network}`

test/e2e/utils/getScores.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,29 @@ import { test, expect, describe } from 'vitest';
22
import { getScores } from '../../../src/utils';
33

44
describe('test getScores', () => {
5-
test('getScores should returns a promise rejection on error from score-api', async () => {
5+
test('getScores should return a promise rejection on error from score-api', async () => {
66
expect.assertions(1);
77
await expect(
88
getScores('test.eth', [], '1', ['0x0'])
99
).to.rejects.toHaveProperty('code');
1010
});
11+
12+
test('getScores should return a promise rejection with JSON-RPC format on network error', async () => {
13+
expect.assertions(1);
14+
await expect(
15+
getScores(
16+
'test.eth',
17+
[],
18+
'1',
19+
[''],
20+
'latest',
21+
'https://score-null.snapshot.org'
22+
)
23+
).to.rejects.toEqual({
24+
code: 'ENOTFOUND',
25+
message:
26+
'FetchError: request to https://score-null.snapshot.org/api/scores failed, reason: getaddrinfo ENOTFOUND score-null.snapshot.org',
27+
data: ''
28+
});
29+
});
1130
});

0 commit comments

Comments
 (0)