Skip to content

Commit 0c5c051

Browse files
authored
fix: do not ignore error if request fails (#578)
1 parent 07e9a3c commit 0c5c051

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
2222
- Cleanup code and refactor to be more efficient
2323
- Correct TS types for working with OpenMetrics
2424
- Updated Typescript and Readme docs for `setToCurrentTime()` to reflect units as seconds.
25+
- Do not ignore error if request to pushgateway fails
2526

2627
### Added
2728

lib/pushgateway.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ async function useGateway(method, job, groupings) {
7272
body += chunk;
7373
});
7474
resp.on('end', () => {
75-
resolve({ resp, body });
75+
if (resp.statusCode >= 400) {
76+
reject(
77+
new Error(`push failed with status ${resp.statusCode}, ${body}`),
78+
);
79+
} else {
80+
resolve({ resp, body });
81+
}
7682
});
7783
});
7884
req.on('error', err => {

test/pushgatewayTest.js

+36
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ describe.each([
6666
expect(mockHttp.isDone());
6767
});
6868
});
69+
70+
it('should throw an error if the push failed', () => {
71+
nock('http://192.168.99.100:9091')
72+
.post('/metrics/job/testJob/key/value', body)
73+
.reply(400);
74+
75+
return expect(
76+
instance.pushAdd({
77+
jobName: 'testJob',
78+
groupings: { key: 'value' },
79+
}),
80+
).rejects.toThrow('push failed with status 400');
81+
});
6982
});
7083

7184
describe('push', () => {
@@ -88,6 +101,19 @@ describe.each([
88101
expect(mockHttp.isDone());
89102
});
90103
});
104+
105+
it('should throw an error if the push failed', () => {
106+
nock('http://192.168.99.100:9091')
107+
.put('/metrics/job/testJob/key/value', body)
108+
.reply(400);
109+
110+
return expect(
111+
instance.push({
112+
jobName: 'testJob',
113+
groupings: { key: 'value' },
114+
}),
115+
).rejects.toThrow('push failed with status 400');
116+
});
91117
});
92118

93119
describe('delete', () => {
@@ -100,6 +126,16 @@ describe.each([
100126
expect(mockHttp.isDone());
101127
});
102128
});
129+
130+
it('should throw an error if the push failed', () => {
131+
nock('http://192.168.99.100:9091')
132+
.delete('/metrics/job/testJob')
133+
.reply(400);
134+
135+
return expect(instance.delete({ jobName: 'testJob' })).rejects.toThrow(
136+
'push failed with status 400',
137+
);
138+
});
103139
});
104140

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

0 commit comments

Comments
 (0)