Merge pull request #283 from hybridgroup/add/syntax-checks

Add basic DSL error checks
This commit is contained in:
Adrian Zankich 2015-04-02 14:05:26 -07:00
commit 2de8f9a44d
1 changed files with 31 additions and 0 deletions

View File

@ -46,6 +46,9 @@ var Robot = module.exports = function Robot(opts) {
}, this);
this.initRobot(opts);
this.checkForBadSyntax(opts);
this.initConnections(opts);
this.initDevices(opts);
@ -153,6 +156,34 @@ Robot.prototype.initRobot = function(opts) {
}
};
// Public: Checks options for bad Cylon syntax
//
// Returns nothing
Robot.prototype.checkForBadSyntax = function(opts) {
var self = this;
var RobotDSLError = new Error("Unable to start robot due to a syntax error");
RobotDSLError.name = "RobotDSLError";
function has(prop) { return opts[prop] != null; }
function checkForSingleObjectSyntax(type) {
var plural = type + "s";
if (has(type) && !has(plural)) {
[
"The single-object '" + type + "' syntax for robots is not valid.",
"Instead, use the multiple-value '" + plural + "' key syntax.",
"Details: http://cylonjs.com/documentation/guides/working-with-robots/"
].forEach(function(str) { self.log("fatal", str); });
throw RobotDSLError;
}
}
["connection", "device"].forEach(checkForSingleObjectSyntax);
};
// Public: Initializes all connections for the robot
//
// opts - options array passed to constructor