Skip to content

Commit 283c361

Browse files
committed
Merge pull request #38 from nselvidge/master
Added better error handling for missing json files
2 parents c2e1759 + aa66c7e commit 283c361

7 files changed

Lines changed: 44 additions & 18 deletions

File tree

lib/commands/host.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var express = require("express");
55
var path = require('path');
66
var fs = require('fs');
7+
var logger = require('../logger');
78

89
var readConfig = require("../read-config");
910
var LocalController = require('../server/controller');
@@ -37,7 +38,12 @@ module.exports = function(varPath, port, program) {
3738
}
3839

3940
//local controller for the requests
40-
localController = new LocalController(variation, port);
41+
try {
42+
localController = new LocalController(variation, port);
43+
} catch(error) {
44+
logger.log("error", error.message);
45+
return;
46+
}
4147

4248
//set the routes
4349
app.get("/", localController.installUserScript.bind(localController));

lib/commands/push-variation.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ module.exports = function(folder, program) {
2626
//find the experiment
2727
this.experiment = new Experiment({}, path.normalize(variation.baseDir +
2828
"/.."));
29-
this.experiment.loadFromFile();
30-
if (!experiment.attributes.id) {
29+
if(!this.experiment.loadFromFile()){
30+
logger.log("error", "no experiment.json found.");
31+
return;
32+
} else if (!this.experiment.attributes.id) {
3133
logger.log("error",
3234
"no id found for experiment. Please run push-experiment first"
3335
);

lib/experiment.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Experiment.locateAndLoad = function(identifier) {
3535
if (fs.existsSync(identifier) && fs.lstatSync(identifier).isDirectory()) {
3636
//it's a directory
3737
experiment = new Experiment({}, identifier);
38-
experiment.loadFromFile();
38+
if(!experiment.loadFromFile()) return false;
3939
} else {
4040
var attrs = {};
4141
glob.sync("**/" + Experiment.JSON_FILE_NAME).forEach(function(jsonFile) {
@@ -50,6 +50,7 @@ Experiment.locateAndLoad = function(identifier) {
5050
}
5151
} catch (e) {
5252
logger.log("warn", "could not parse " + jsonFile);
53+
return false;
5354
}
5455
})
5556
}

lib/file-util.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,27 @@ module.exports = {
2121
},
2222
loadConfigItem: function(fileName) {
2323
var configItem;
24-
configItem = JSON.parse(
25-
fs.readFileSync(fileName, {
26-
encoding: "utf-8"
27-
}));
28-
return configItem;
24+
if (fs.existsSync(fileName)){
25+
configItem = JSON.parse(
26+
fs.readFileSync(fileName, {
27+
encoding: "utf-8"
28+
}));
29+
return configItem;
30+
} else {
31+
return false;
32+
}
33+
2934
},
3035
loadFile: function(fileName) {
3136
var theFile;
32-
theFile = fs.readFileSync(fileName, {
33-
encoding: "utf-8"
34-
});
35-
return theFile;
37+
if (fs.existsSync(fileName)){
38+
theFile = fs.readFileSync(fileName, {
39+
encoding: "utf-8"
40+
});
41+
return theFile;
42+
} else {
43+
return false;
44+
}
3645
}
3746
}
3847

lib/optcli-base.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ OptCLIBase.prototype.getJSONPath = function() {
2727
}
2828

2929
OptCLIBase.prototype.loadFromFile = function() {
30-
this.attributes = fileUtil.loadConfigItem(this.getJSONPath()) || {};
31-
return this.attributes;
30+
if (this.JSONFileExists()){
31+
this.attributes = fileUtil.loadConfigItem(this.getJSONPath()) || {};
32+
return this.attributes;
33+
} else return false;
34+
3235
}
3336

3437
OptCLIBase.prototype.JSONFileExists = function() {

lib/project.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ util.inherits(Project, OptCLIBase);
1515

1616
Project.createFromFile = function() {
1717
var project = new Project({}, "./");
18-
project.loadFromFile();
18+
if(!project.loadFromFile()) return false;
1919
return project;
20+
2021
}
2122

2223
Project.prototype.save = function() {

lib/server/controller.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ function LocalController(variation, port) {
1616
//assume the directory above the variation
1717
//maybe move this to Experiment?
1818
this.experiment = new Experiment({}, path.normalize(variation.baseDir + "/.."));
19-
this.experiment.loadFromFile();
19+
if(!this.experiment.loadFromFile()) {
20+
throw new Error("no experiment.json found");
21+
}
2022
this.project = new Project({}, "./");
21-
this.project.loadFromFile();
23+
if(!this.project.loadFromFile()) {
24+
throw new Error("no project.json found");
25+
}
2226
//assume assets are in experiment.baseDir
2327
this.assets = new Assets({}, this.experiment.baseDir);
2428
if (this.assets.JSONFileExists()) {

0 commit comments

Comments
 (0)