Permalink
Browse files

Added better error handling in the case of failed HTTP requests, and …

…broken js code. Added support for <foreach> iterating through objects.
  • Loading branch information...
1 parent fa45552 commit aa0d8ed89698ccc66ee554cfe3945cff9d8fd65b @jbeard4 committed Aug 29, 2012
@@ -83,7 +83,14 @@ function linkReferencesAndGenerateActionFactory(json){
function annotatedJsonToModel(json,documentUrl) {
var actionFactoryString = linkReferencesAndGenerateActionFactory(json);
- json.actionFactory = pm.platform.eval(actionFactoryString,documentUrl);
+ try {
+ json.actionFactory = pm.platform.eval(actionFactoryString,documentUrl);
+ }catch(e){
+ pm.platform.log("Failed to evaluate action factory.");
+ pm.platform.log("Generated js code to evaluate\n",actionFactoryString);
+ require('fs').writeFileSync('out.js',actionFactoryString,'utf8');
+ throw e;
+ }
}
module.exports = function(json,documentUrl){
@@ -63,13 +63,11 @@ var transform = exports.transform = function(scxmlDoc) {
transitions.filter(function(t){return t.targets;}).forEach(function(transition){
var source = idToStateMap[transition.source];
- var targets = transition.targets.map(function(target){return idToStateMap[target];});
-
- if (!source) {
- throw new Error("source missing");
- } else if (!targets.length) {
- throw new Error("target missing");
- }
+ var targets = transition.targets.map(function(target){
+ var state = idToStateMap[target];
+ if(!state) throw new Error("Transition targets state id '" + target + "' but state does not exist.");
+ return state;
+ });
transition.lca = getLCA(source, targets[0]);
});
View
@@ -133,13 +133,21 @@ var actionTags = {
var isIndexDefined = pm.platform.dom.hasAttribute(action,"index"),
index = pm.platform.dom.getAttribute(action,"index") || "$i", //FIXME: the index variable could shadow the datamodel. We should pick a unique temperorary variable name
item = pm.platform.dom.getAttribute(action,"item"),
- arr = pm.platform.dom.getAttribute(action,"array");
+ arr = pm.platform.dom.getAttribute(action,"array"),
+ foreachBody = pm.platform.dom.getElementChildren(action).map(actionTagToFnBody).join("\n;;\n");
return "(function(){\n" +
- "for(" + (isIndexDefined ? "" : "var " + index + " = 0") + "; " + index + " < " + arr + ".length; " + index + "++){\n" +
- item + " = " + arr + "[" + index + "];\n" +
- pm.platform.dom.getElementChildren(action).map(actionTagToFnBody).join("\n;;\n") +
- "\n}\n" +
+ "if(Array.isArray(" + arr + ")){\n" +
+ arr + ".forEach(function(" + item + "," + index + "){\n" +
+ foreachBody +
+ "\n});\n" +
+ "}else{\n" +
+ //assume object
+ "Object.keys(" + arr + ").forEach(function(" + index + "){\n" +
+ item + " = " + arr + "[" + index + "];\n" +
+ foreachBody +
+ "\n});\n" +
+ "}\n" +
"})();";
}
}
@@ -227,7 +235,12 @@ function constructSendEventData(action){
if(content.length){
//TODO: instead of using textContent, serialize the XML
- return JSON.stringify(content.map(function(child){return pm.platform.dom.textContent(child);})[0]);
+ content = content[0];
+ if(pm.platform.dom.getAttribute(content,'type') === 'application/json'){
+ return pm.platform.dom.textContent(content);
+ }else{
+ return JSON.stringify(pm.platform.dom.textContent(content));
+ }
}else if(pm.platform.dom.hasAttribute(action,"contentexpr")){
return pm.platform.dom.getAttribute(action,"contentexpr");
}else{
@@ -72,10 +72,11 @@ function inlineSrcs(url,doc,cb){
pm.platform.getResourceFromUrl(scriptUrl,function(err,text){
if(err){
//just capture the error, and continue on
+ pm.platform.log("Error downloading document " + scriptUrl + " : " + err.message);
errors.push(err);
+ }else{
+ pm.platform.dom.textContent(script,text);
}
-
- pm.platform.dom.textContent(script,text);
retrieveScripts();
});
}else{
View
@@ -32,7 +32,11 @@ function httpGet(url,cb){
s += d;
});
res.on('end',function(){
- cb(null,s);
+ if(res.statusCode === 200){
+ cb(null,s);
+ }else{
+ cb(new Error('HTTP code ' + res.statusCode + ' : ' + s));
+ }
});
}).on('error', function(e) {
cb(e);

0 comments on commit aa0d8ed

Please sign in to comment.