Skip to content

Commit e166711

Browse files
Don't rely on on res being defined
1 parent 1958dbf commit e166711

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/api.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ function get(config, uri, done) {
3636
per_page: 100
3737
})
3838
.set('User-Agent', 'StriderCD (http://stridercd.com)')
39-
.end(function (error, response) {
40-
if (response.error) {
41-
return done(response.error, null);
39+
.end(function (error, res) {
40+
res = res || {};
41+
if (res.error) {
42+
return done(res.error, null);
4243
}
43-
if (!response.body) {
44+
if (!res.body) {
4445
return done();
4546
}
46-
done(null, response.body);
47+
done(null, res.body);
4748
});
4849
}
4950

@@ -80,7 +81,7 @@ function createHooks(config, repo_id, url, callback) {
8081
})
8182
.end(function (err, res) {
8283
if (err) return callback(err);
83-
if (res.status !== 201) return callback(res.status);
84+
if (res && res.status !== 201) return callback(res.status);
8485

8586
return callback(null, true);
8687
});
@@ -97,6 +98,10 @@ function deleteHooks(config, repo_id, url, callback) {
9798
.end(function (err, res) {
9899
var deleted = false;
99100

101+
if (!res) {
102+
return callback(new Error("Empty response."));
103+
}
104+
100105
async.each(res.body, function (hook, cb) {
101106
// Remove all webhooks matching url
102107
if (hook.url == url) {
@@ -129,7 +134,7 @@ function addDeployKey(config, repo_id, title, key, callback) {
129134
})
130135
.end(function (err, res) {
131136
if (err) return callback(err);
132-
if (res.status !== 201) return callback(res.status);
137+
if (res && res.status !== 201) return callback(res.status);
133138

134139
return callback(null, true)
135140
});
@@ -146,6 +151,10 @@ function removeDeployKey(config, repo_id, title, callback) {
146151
.end(function (err, res) {
147152
var deleted = false;
148153

154+
if (!res) {
155+
return callback(new Error("Empty response."));
156+
}
157+
149158
async.each(res.body, function (key, cb) {
150159
// Remove all webhooks matching url
151160
if (key.title == title) {

0 commit comments

Comments
 (0)