Skip to content

Commit

Permalink
fixes for testing and preventing bot from returning errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zirkerc committed Apr 27, 2018
1 parent ff58877 commit 9071c39
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion leo-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ program
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}
}
4 changes: 2 additions & 2 deletions lib/cloud-formation.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {
"Fn::Sub": `table/\${${tableName}}`,
},
RoleARN: {
"Fn::Sub": "${ScalingRole.Arn}"
"Fn::Sub": "${AutoScalingRole.Arn}"
},
"ScalableDimension": `dynamodb:table:${type}CapacityUnits`,
"ServiceNamespace": "dynamodb"
Expand Down Expand Up @@ -1239,4 +1239,4 @@ function getSwagger(cloudFormation, microservice) {
}
}
};
}
}
3 changes: 2 additions & 1 deletion lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require("./leo-bot-run.js")(argv, (err, module) => {
let doCall = () => {
module.handler(module.event, createContext({}, module.config), (err, data) => {
module.runner.callback(err, data, (err, data) => {
err = err || (data instanceof Error ? data : undefined)
data && console.log("\n\n\n--------------------------Results--------------------------\n")
let results = data;
if (err) {
Expand Down Expand Up @@ -109,4 +110,4 @@ function stripBOM(content) {
content = content.slice(1);
}
return content;
}
}
11 changes: 6 additions & 5 deletions lib/showpages.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = function(config, version, pages) {
var p = pages.map(function(e) {
return "/" + e;
});
var regex = "\\\\${leo\.(.*?)}";
return `"use strict"
var p = pages.map(function(e) {
return "/" + e;
});
var regex = "\\\\${leo\.(.*?)}";
return `"use strict"
var fs = require("fs");
var configure = ${JSON.stringify(config)};
let r = process.resources = process.env.Resources && JSON.parse(process.env.Resources) || {};
Expand Down Expand Up @@ -70,6 +70,7 @@ exports.handler = function (event, context, callback) {
configure.static.uri = configure.static.cloudfront + fullAppName + '/' + version + '/';
configure.custom_css = process.env.CustomCSS || "";
configure.custom_js = process.env.CustomJS || "";
configure.baseHref = baseRef;
if(['${p.join("','")}'].indexOf(page) !== -1) {
fs.readFile("./pages/"+page, 'utf8', function (err, data) {
Expand Down
5 changes: 3 additions & 2 deletions lib/wrappers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let decrypted = false;
let botHandler = function(event, context, callback) {
let tasks = [];
Object.keys(process.env).forEach(function(key) {
if (!decrypted && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
if (!decrypted && process.env[key] != undefined && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
tasks.push(function(done) {
kms.decryptString(process.env[key].replace(/^KMS:/, ""), function(err, value) {
if (err) {
Expand Down Expand Up @@ -85,11 +85,12 @@ module.exports = {
});
//clear out the registry
empty(config.registry);
leosdk.configuration.registry = config.registry;
config.registry.context = context;
if (!config.registry.id) { //If they didn't specify it in their config, then let's get it from the function name
config.registry.id = process.env.AWS_LAMBDA_FUNCTION_NAME;
}

botHandler(event, context, callback);
}
};
};
19 changes: 10 additions & 9 deletions lib/wrappers/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let decrypted = false;
let botHandler = function(event, context, callback) {
let tasks = [];
Object.keys(process.env).forEach(function(key) {
if (!decrypted && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
if (!decrypted && process.env[key] != undefined && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
tasks.push(function(done) {
kms.decryptString(process.env[key].replace(/^KMS:/, ""), function(err, value) {
if (err) {
Expand Down Expand Up @@ -75,7 +75,7 @@ process.on('uncaughtException', function(err) {
}
console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
theCallback("Application Error");
theCallback(null, "Application Error");
});

function empty(obj) {
Expand Down Expand Up @@ -124,6 +124,7 @@ module.exports = {
theCallback = callback;
//clear out the registry
empty(config.registry);
leosdk.configuration.registry = config.registry;
config.registry.context = context;
config.registry.__cron = event.__cron;
global.cron_run_again = false;
Expand Down Expand Up @@ -154,12 +155,12 @@ module.exports = {
if (err || err2) {
console.log(err || err2);
}
callback(err, data);
callback(null, err || data);
});
});
}).catch(err => {
cron.reportComplete(event.__cron, context.awsRequestId, "error", err, {}, function() {
callback(err);
callback(null, err);
});
});
} catch (e) {
Expand All @@ -168,7 +169,7 @@ module.exports = {
msg: e.message,
stack: e.stack
}, {}, function() {
callback(e);
callback(null, e);
});
}

Expand All @@ -192,24 +193,24 @@ module.exports = {
if (err || err2) {
console.log(err || err2);
}
callback(err, data);
callback(null, err || data);
});
});
}).catch(err => {
debug && console.log("error");
cron.removeLock(config.name, context.awsRequestId, function() {
callback(err);
callback(null, err);
}, "error");
});
} catch (e) {
debug && console.log("error");
cron.removeLock(config.name, context.awsRequestId, function() {
callback(e);
callback(null, e);
});
}

}
});
}
}
};
};
4 changes: 2 additions & 2 deletions lib/wrappers/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let decrypted = false;
let botHandler = function(event, context, callback) {
let tasks = [];
Object.keys(process.env).filter(e => !e.match(/^npm/)).forEach(function(key) {
if (!decrypted && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
if (!decrypted && process.env[key] != undefined && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
tasks.push(function(done) {
kms.decryptString(process.env[key].replace(/^KMS:/, ""), function(err, value) {
if (err) {
Expand Down Expand Up @@ -59,4 +59,4 @@ module.exports = {

return botHandler(event, context, callback);
}
};
};
5 changes: 3 additions & 2 deletions lib/wrappers/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let decrypted = false;
let botHandler = function(event, context, callback) {
let tasks = [];
Object.keys(process.env).forEach(function(key) {
if (!decrypted && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
if (!decrypted && process.env[key] != undefined && (key.toLowerCase().indexOf('kms') !== -1 || process.env[key].match(/^KMS:/)) && !key.match(/^npm_/)) {
tasks.push(function(done) {
kms.decryptString(process.env[key].replace(/^KMS:/, ""), function(err, value) {
if (err) {
Expand Down Expand Up @@ -78,6 +78,7 @@ module.exports = {

//clear out the registry
empty(config.registry);
leosdk.configuration.registry = config.registry;
config.registry.user = null;

for (let x of process.listeners('uncaughtException')) { //remove lambdas default listener
Expand Down Expand Up @@ -152,4 +153,4 @@ module.exports = {
});
});
}
};
};

0 comments on commit 9071c39

Please sign in to comment.