Add basic DSL error checks

This commit is contained in:
Andrew Stewart 2015-04-02 12:22:19 -07:00
parent eb5090e9e9
commit 9b04a4ed15
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