Skip to content

Commit 7ad3b45

Browse files
committed
Actual fix for a.)crash on incorrect host b.)superagent gives us objects directly
The first commit's message is: Crash on receiving bad data when listing projects When there was a problem in the gitlab configuration and an unreachable/incorrect hostname was specified, instead of JSON, we were getting undefined from the get method in api.js, and we were crashing. Added an error message for this case so that we do not crash. Ideally we should be checking for a valid reachable gitlab host when saving the configuration. Crash trace below: SyntaxError: Unexpected token u at Object.parse (native) at /home/dev/sandbox/strider/node_modules/strider-gitlab/lib/webapp.js:34:20 at /home/dev/sandbox/strider/node_modules/strider-gitlab/lib/api.js:45:16 at Request.callback (/home/dev/sandbox/strider/node_modules/strider-gitlab/node_modules/superagent/lib/node/index.js:628:30) at ClientRequest.<anonymous> (/home/dev/sandbox/strider/node_modules/strider-gitlab/node_modules/superagent/lib/node/index.js:596:10) at ClientRequest.emit (events.js:107:17) at Socket.socketErrorListener (_http_client.js:271:9) at Socket.emit (events.js:107:17) at net.js:459:14 at process._tickCallback (node.js:355:11) 9284 died 1 null This is the 2nd commit message: superagent already parses JSON into objects for us superagent is parsing the JSON received from the API into Objects per - https://visionmedia.github.io/superagent/#parsing-response%20bodies, so in webapp.js, branches does not contain a JSON/string but an Object. JSON.parse(branches) is failing with Unexpected token o JSON.parse is perhaps not required - when getting a list of projects and also when getting a list of branches. With the code as it stands, we crash when trying to configure a project. 2015-08-05T05:05:48.543Z - error: strider-gitlab:webapp Getting URI projects/2/repository/branches +0ms 2015-08-05T05:05:48.573Z - error: strider-gitlab:api Response body type: object +0ms undefined:1 [object Object] ^ SyntaxError: Unexpected token o at Object.parse (native) at /home/dev/sandbox/strider/node_modules/strider-gitlab/lib/webapp.js:68:23 at /home/dev/sandbox/strider/node_modules/strider-gitlab/lib/api.js:50:7 at Request.callback (/home/dev/sandbox/strider/node_modules/strider-gitlab/node_modules/superagent/lib/node/index.js:628:30) at Request.<anonymous> (/home/dev/sandbox/strider/node_modules/strider-gitlab/node_modules/superagent/lib/node/index.js:131:10) at Request.emit (events.js:107:17) at Stream.<anonymous> (/home/dev/sandbox/strider/node_modules/strider-gitlab/node_modules/superagent/lib/node/index.js:773:12) at Stream.emit (events.js:129:20) at Unzip.<anonymous> (/home/dev/sandbox/strider/node_modules/strider-gitlab/node_modules/superagent/lib/node/utils.js:124:12) at Unzip.emit (events.js:129:20) at _stream_readable.js:908:16 at process._tickCallback (node.js:355:11) 20518 died 1 null
1 parent 492e40e commit 7ad3b45

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/api.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var _ = require('lodash');
22
var async = require('async');
33
var superagent = require('superagent');
4+
var debug = require('debug')('strider-gitlab:api');
45

56
module.exports = {
67
get: get,
@@ -38,6 +39,8 @@ function get(config, uri, done) {
3839
.set('User-Agent', 'StriderCD (http://stridercd.com)')
3940
.end(function (error, res) {
4041
res = res || {};
42+
debug('Response body type: ' + typeof res.body);
43+
debug('Response body received: ' + JSON.stringify(res.body));
4144
if (res.error) {
4245
return done(res.error, null, res);
4346
}

lib/webapp.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var api = require('./api');
33
var async = require('async');
44
var util = require('util');
55
var webhooks = require('./webhooks');
6-
6+
var debug = require('debug')('strider-gitlab:webapp');
77
var hostname = process.env.strider_server_name || 'http://localhost:3000';
88

99
module.exports = {
@@ -30,7 +30,9 @@ module.exports = {
3030
api.get(config, 'projects', function (err, repos) {
3131
if (err) return done(err);
3232

33-
repos = JSON.parse(repos);
33+
if(!repos) {
34+
return done(new Error("Could not get a list of projects from the GitLab repository. Please check the configuration in Account->GitLab."));
35+
}
3436

3537
// Parse and filter only git repos
3638
done(null, repos.map(api.parseRepo).filter(function (repo) {
@@ -55,9 +57,9 @@ module.exports = {
5557

5658
var uri = util.format('projects/%d/repository/branches', repo_id);
5759

60+
debug("Getting URI " + uri);
5861
api.get(account, uri, function (err, branches) {
59-
60-
branches = JSON.parse(branches);
62+
debug("We get the following as branches: " + JSON.stringify(branches));
6163

6264
done(err, _.map(branches || [], function (branch) {
6365
return branch.name

0 commit comments

Comments
 (0)