From cdffea143ced190e1cd00c2021db60e4231fbc6e Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 16 May 2017 14:53:15 +1000 Subject: [PATCH 1/2] Catch errors within the project to avoid Cloudformation getting stuck for 1 hour --- lib/project/handler.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/project/handler.js b/lib/project/handler.js index 8c3e0f5..517d962 100644 --- a/lib/project/handler.js +++ b/lib/project/handler.js @@ -38,7 +38,11 @@ module.exports = function handler(event,context) { if (!_.includes(getDirectories(resourcePath), resourceType)) return util.done("Invalid resourceType:" + resourceType, ev, context); - require(path.join(resourcePath,resourceType)).handler(ev,context); + try { + require(path.join(resourcePath, resourceType)).handler(ev, context); + } catch(e) { + util.done(e,ev,context); + } }; function getDirectories(srcpath) { From 70b190731312e5fef37f33df94dacdd6c32bc6de Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 16 May 2017 16:37:33 +1000 Subject: [PATCH 2/2] Add tests --- .../lib/resources/resource3/create.js | 15 +++++++++++++ .../lib/resources/resource3/delete.js | 7 ++++++ .../project1/lib/resources/resource3/index.js | 5 +++++ .../lib/resources/resource3/update.js | 7 ++++++ test/integration/project1/project.test.js | 22 +++++++++++++++++++ 5 files changed, 56 insertions(+) create mode 100644 test/fixtures/project1/lib/resources/resource3/create.js create mode 100644 test/fixtures/project1/lib/resources/resource3/delete.js create mode 100644 test/fixtures/project1/lib/resources/resource3/index.js create mode 100644 test/fixtures/project1/lib/resources/resource3/update.js diff --git a/test/fixtures/project1/lib/resources/resource3/create.js b/test/fixtures/project1/lib/resources/resource3/create.js new file mode 100644 index 0000000..354ac9f --- /dev/null +++ b/test/fixtures/project1/lib/resources/resource3/create.js @@ -0,0 +1,15 @@ +var create = require('../../../../../../lib/resource/create') +util = require('../../../../../../lib/util'); + +module.exports.handler = function(event,context) { + create.apply(this,[event,context,myCreate]); +}; + +var myCreate = function(err,event,context) { + if (err) + return util.done(err); + + dont_fail_badly(); + + util.done(null,event,context,{id: 1, name: "First Resource"},1); +}; diff --git a/test/fixtures/project1/lib/resources/resource3/delete.js b/test/fixtures/project1/lib/resources/resource3/delete.js new file mode 100644 index 0000000..79d185d --- /dev/null +++ b/test/fixtures/project1/lib/resources/resource3/delete.js @@ -0,0 +1,7 @@ +var destroy = require('../../../../../../lib/resource/delete') + util = require('../../../../../../lib/util'); + +// Bad function, no callback +module.exports.handler = function(event, context) { + destroy.apply(this, [event, context]); +}; diff --git a/test/fixtures/project1/lib/resources/resource3/index.js b/test/fixtures/project1/lib/resources/resource3/index.js new file mode 100644 index 0000000..6aa73b4 --- /dev/null +++ b/test/fixtures/project1/lib/resources/resource3/index.js @@ -0,0 +1,5 @@ +var handler = require('../../../../../../lib/resource/handler'); + +module.exports.handler = function() { + handler.apply(this,arguments); +}; diff --git a/test/fixtures/project1/lib/resources/resource3/update.js b/test/fixtures/project1/lib/resources/resource3/update.js new file mode 100644 index 0000000..468f7aa --- /dev/null +++ b/test/fixtures/project1/lib/resources/resource3/update.js @@ -0,0 +1,7 @@ +var update = require('../../../../../../lib/resource/update') + util = require('../../../../../../lib/util'); + +// Bad function, no callback +module.exports.handler = function(event, context) { + update.apply(this, [event, context]); +}; diff --git a/test/integration/project1/project.test.js b/test/integration/project1/project.test.js index e32edb9..311b6a0 100644 --- a/test/integration/project1/project.test.js +++ b/test/integration/project1/project.test.js @@ -261,5 +261,27 @@ describe('project1', function() { }); }); }); + + describe("resource3", function() { + + describe("create", function() { + + it("should run without error", function(cb) { + var event = { + resourceType: "resource3", + requestType: "create" + }; + + var context = { + done: function(err,data,id) { + assert.equal(err, 'ReferenceError: dont_fail_badly is not defined'); + cb(); + } + }; + project1.handler(event,context); + }); + }); + + }); }); });