Skip to content

Commit

Permalink
fix: do not ignore error if request fails (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeberk authored Sep 11, 2023
1 parent 07e9a3c commit 0c5c051
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
- Cleanup code and refactor to be more efficient
- Correct TS types for working with OpenMetrics
- Updated Typescript and Readme docs for `setToCurrentTime()` to reflect units as seconds.
- Do not ignore error if request to pushgateway fails

### Added

Expand Down
8 changes: 7 additions & 1 deletion lib/pushgateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ async function useGateway(method, job, groupings) {
body += chunk;
});
resp.on('end', () => {
resolve({ resp, body });
if (resp.statusCode >= 400) {
reject(
new Error(`push failed with status ${resp.statusCode}, ${body}`),
);
} else {
resolve({ resp, body });
}
});
});
req.on('error', err => {
Expand Down
36 changes: 36 additions & 0 deletions test/pushgatewayTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ describe.each([
expect(mockHttp.isDone());
});
});

it('should throw an error if the push failed', () => {
nock('http://192.168.99.100:9091')
.post('/metrics/job/testJob/key/value', body)
.reply(400);

return expect(
instance.pushAdd({
jobName: 'testJob',
groupings: { key: 'value' },
}),
).rejects.toThrow('push failed with status 400');
});
});

describe('push', () => {
Expand All @@ -88,6 +101,19 @@ describe.each([
expect(mockHttp.isDone());
});
});

it('should throw an error if the push failed', () => {
nock('http://192.168.99.100:9091')
.put('/metrics/job/testJob/key/value', body)
.reply(400);

return expect(
instance.push({
jobName: 'testJob',
groupings: { key: 'value' },
}),
).rejects.toThrow('push failed with status 400');
});
});

describe('delete', () => {
Expand All @@ -100,6 +126,16 @@ describe.each([
expect(mockHttp.isDone());
});
});

it('should throw an error if the push failed', () => {
nock('http://192.168.99.100:9091')
.delete('/metrics/job/testJob')
.reply(400);

return expect(instance.delete({ jobName: 'testJob' })).rejects.toThrow(
'push failed with status 400',
);
});
});

describe('when using basic authentication', () => {
Expand Down

0 comments on commit 0c5c051

Please sign in to comment.