Skip to content

Commit 6c69e89

Browse files
nlfsatazor
authored andcommitted
fix: ignore invalid json responses (#29)
1 parent 9bbd0eb commit 6c69e89

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/util/shieldsIo.js

+8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ function request(url, options) {
6161
const match = json.value.match(/^(\d+)%$/);
6262

6363
return match ? Number(match[1]) / 100 : null;
64+
})
65+
.catch((err) => {
66+
// Swallow ParseErrors (i.e. invalid json payloads, but valid responses) and return null
67+
if (err instanceof got.ParseError) {
68+
return null;
69+
}
70+
71+
throw err;
6472
});
6573
}
6674

test/test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ afterEach(() => {
88
nock.cleanAll();
99
});
1010

11-
it('should return null if repository is not supported or if the URL is malformed', async () => {
11+
it('should return null if repository is not supported or if the URL is malformed or response is not json', async () => {
1212
nock.disableNetConnect();
1313

1414
let promise = fetchCoverage('git@foo.com:user/project.git');
@@ -22,6 +22,16 @@ it('should return null if repository is not supported or if the URL is malformed
2222

2323
expect(promise instanceof Promise).toBe(true);
2424
expect(coverage).toBe(null);
25+
26+
nock('https://img.shields.io')
27+
.get('/codecov/c/github/user/project.json')
28+
.reply(200, '<this is not json>');
29+
30+
promise = fetchCoverage('git@github.com:user/project.git', { services: ['codecov'] });
31+
coverage = await promise;
32+
33+
expect(promise instanceof Promise).toBe(true);
34+
expect(coverage).toBe(null);
2535
});
2636

2737
it('should try appropriate services for GitHub repositories', async () => {

0 commit comments

Comments
 (0)