diff --git a/CHANGELOG.md b/CHANGELOG.md index 4912f342..2137ef55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/pushgateway.js b/lib/pushgateway.js index cbc22439..0c916cd7 100644 --- a/lib/pushgateway.js +++ b/lib/pushgateway.js @@ -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 => { diff --git a/test/pushgatewayTest.js b/test/pushgatewayTest.js index f61f480e..8b711efe 100644 --- a/test/pushgatewayTest.js +++ b/test/pushgatewayTest.js @@ -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', () => { @@ -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', () => { @@ -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', () => {