From 6f400e4c45dc547de342766f3f20d2e1a8ed5886 Mon Sep 17 00:00:00 2001 From: EvilDrW Date: Sat, 5 Dec 2015 20:59:39 -0600 Subject: [PATCH 1/5] add deployd's error handling to dpd-event --- index.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0d4141f..a0a0dc3 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,9 @@ EventResource.prototype.handle = function (ctx, next) { var parts = ctx.url.split('/').filter(function(p) { return p; }); var result = {}; + + var errors = {}; + var hasErrors = false; var domain = { url: ctx.url @@ -38,23 +41,43 @@ EventResource.prototype.handle = function (ctx, next) { , setResult: function(val) { result = val; } + , error: function(key, val) { + debug('error %s %s', key, val); + errors[key] = val || true; + hasErrors = true; + } + , errorIf: function(condition, key, value) { + if (condition) { + domain.error(key, value); + } + } + , errorUnless: function(condition, key, value) { + domain.errorIf(!condition, key, value); + } + , hasErrors: function() { + return hasErrors; + } }; if (ctx.method === "POST" && this.events.post) { this.events.post.run(ctx, domain, function(err) { - ctx.done(err, result); + if(err || domain.hasErrors()) return done(err || errors); + ctx.done(null, result); }); } else if (ctx.method === "GET" && this.events.get) { this.events.get.run(ctx, domain, function(err) { - ctx.done(err, result); + if(err || domain.hasErrors()) return done(err || errors); + ctx.done(null, result); }); } else if (ctx.method === "DELETE" && this.events.delete) { this.events.delete.run(ctx, domain, function(err) { - ctx.done(err, result); + if(err || domain.hasErrors()) return done(err || errors); + ctx.done(null, result); }); } else if (ctx.method === "PUT" && this.events.put) { this.events.put.run(ctx, domain, function(err) { - ctx.done(err, result); + if(err || domain.hasErrors()) return done(err || errors); + ctx.done(null, result); }); } else { next(); From fd06c19f5867f4dbdbcc4351bb3ee8cc41cce885 Mon Sep 17 00:00:00 2001 From: EvilDrW Date: Sat, 5 Dec 2015 21:30:34 -0600 Subject: [PATCH 2/5] minor corrections --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a0a0dc3..659451d 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,6 @@ EventResource.prototype.handle = function (ctx, next) { result = val; } , error: function(key, val) { - debug('error %s %s', key, val); errors[key] = val || true; hasErrors = true; } @@ -61,22 +60,22 @@ EventResource.prototype.handle = function (ctx, next) { if (ctx.method === "POST" && this.events.post) { this.events.post.run(ctx, domain, function(err) { - if(err || domain.hasErrors()) return done(err || errors); + if(err || domain.hasErrors()) return ctx.done(err || errors); ctx.done(null, result); }); } else if (ctx.method === "GET" && this.events.get) { this.events.get.run(ctx, domain, function(err) { - if(err || domain.hasErrors()) return done(err || errors); + if(err || domain.hasErrors()) return ctx.done(err || errors); ctx.done(null, result); }); } else if (ctx.method === "DELETE" && this.events.delete) { this.events.delete.run(ctx, domain, function(err) { - if(err || domain.hasErrors()) return done(err || errors); + if(err || domain.hasErrors()) return ctx.done(err || errors); ctx.done(null, result); }); } else if (ctx.method === "PUT" && this.events.put) { this.events.put.run(ctx, domain, function(err) { - if(err || domain.hasErrors()) return done(err || errors); + if(err || domain.hasErrors()) return ctx.done(err || errors); ctx.done(null, result); }); } else { From 78ea25f03181436cd7932f6b7c44f9a860eb90a0 Mon Sep 17 00:00:00 2001 From: EvilDrW Date: Sat, 2 Jan 2016 12:03:05 -0600 Subject: [PATCH 3/5] expose require function to scripts --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 659451d..ba78e96 100644 --- a/index.js +++ b/index.js @@ -56,6 +56,9 @@ EventResource.prototype.handle = function (ctx, next) { , hasErrors: function() { return hasErrors; } + , require: function(module) { // expose require function + return require(module); + } }; if (ctx.method === "POST" && this.events.post) { @@ -83,4 +86,4 @@ EventResource.prototype.handle = function (ctx, next) { } -}; \ No newline at end of file +}; From 44698a754465b217e076c61f57b777b94259e2ad Mon Sep 17 00:00:00 2001 From: EvilDrW Date: Sat, 2 Jan 2016 12:53:22 -0600 Subject: [PATCH 4/5] handle promise results from script execution --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ba78e96..97c6ca2 100644 --- a/index.js +++ b/index.js @@ -64,22 +64,22 @@ EventResource.prototype.handle = function (ctx, next) { if (ctx.method === "POST" && this.events.post) { this.events.post.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - ctx.done(null, result); + Promise.resolve(result).then(function(r) { ctx.done(null, result)}); }); } else if (ctx.method === "GET" && this.events.get) { this.events.get.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - ctx.done(null, result); + Promise.resolve(result).then(function(r) { ctx.done(null, result)}); }); } else if (ctx.method === "DELETE" && this.events.delete) { this.events.delete.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - ctx.done(null, result); + Promise.resolve(result).then(function(r) { ctx.done(null, result)}); }); } else if (ctx.method === "PUT" && this.events.put) { this.events.put.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - ctx.done(null, result); + Promise.resolve(result).then(function(r) { ctx.done(null, result)}); }); } else { next(); From 6bf75a5c4b8cd3946bcabc440e18d438a03f0d55 Mon Sep 17 00:00:00 2001 From: EvilDrW Date: Sat, 2 Jan 2016 12:57:07 -0600 Subject: [PATCH 5/5] correct promise return value --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 97c6ca2..2f8df0c 100644 --- a/index.js +++ b/index.js @@ -64,22 +64,22 @@ EventResource.prototype.handle = function (ctx, next) { if (ctx.method === "POST" && this.events.post) { this.events.post.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - Promise.resolve(result).then(function(r) { ctx.done(null, result)}); + Promise.resolve(result).then(function(r) { ctx.done(null, r)}); }); } else if (ctx.method === "GET" && this.events.get) { this.events.get.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - Promise.resolve(result).then(function(r) { ctx.done(null, result)}); + Promise.resolve(result).then(function(r) { ctx.done(null, r)}); }); } else if (ctx.method === "DELETE" && this.events.delete) { this.events.delete.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - Promise.resolve(result).then(function(r) { ctx.done(null, result)}); + Promise.resolve(result).then(function(r) { ctx.done(null, r)}); }); } else if (ctx.method === "PUT" && this.events.put) { this.events.put.run(ctx, domain, function(err) { if(err || domain.hasErrors()) return ctx.done(err || errors); - Promise.resolve(result).then(function(r) { ctx.done(null, result)}); + Promise.resolve(result).then(function(r) { ctx.done(null, r)}); }); } else { next();