From 650eec0bb37424d25e84e1aae853a082a352f06d Mon Sep 17 00:00:00 2001 From: Darren Demicoli Date: Thu, 27 Jun 2019 12:20:08 +0200 Subject: [PATCH] Throw errors on failures when parsing results and return rejection to serverless --- deploy/cloudflareDeploy.js | 47 +++++++++++++------------- deploy/cloudflareDeployFunction.js | 53 ++++++++++++++++-------------- deploy/lib/logResponse.js | 4 +-- utils/index.js | 35 +++++++++++++------- 4 files changed, 79 insertions(+), 60 deletions(-) diff --git a/deploy/cloudflareDeploy.js b/deploy/cloudflareDeploy.js index 8090476..d59efc2 100644 --- a/deploy/cloudflareDeploy.js +++ b/deploy/cloudflareDeploy.js @@ -38,30 +38,33 @@ class CloudflareDeploy { this.hooks = { "deploy:deploy": () => - BB.bind(this) - .then(this.checkAccountType) - .then(async isMultiScript => { - this.serverless.cli.log('Starting Serverless Cloudflare-Worker deployment.'); - if (isMultiScript && await duplicate.checkIfDuplicateRoutes(this.serverless, this.provider)) { - return BB.reject("Duplicate routes pointing to different script"); - } + BB.bind(this) + .then(this.checkAccountType) + .then(async isMultiScript => { + this.serverless.cli.log('Starting Serverless Cloudflare-Worker deployment.'); + if (isMultiScript && await duplicate.checkIfDuplicateRoutes(this.serverless, this.provider)) { + return BB.reject("Duplicate routes pointing to different script"); + } - if (this.getInvalidScriptNames()) { - return BB.reject( - "Worker names can contain lowercase letters, numbers, underscores, and dashes. They cannot start with dashes." - ); - } + if (this.getInvalidScriptNames()) { + return BB.reject( + "Worker names can contain lowercase letters, numbers, underscores, and dashes. They cannot start with dashes." + ); + } - if (isMultiScript) { - return this.multiScriptDeployAll() - } else { - const functionObject = this.getFunctionObjectForSingleScript(); - return this.deploySingleScript(functionObject); - } - }) - .then(this.logDeployResponse) - .then(k => this.serverless.cli.log(`Finished deployment in ${(Date.now() - startTime) / 1000} seconds.`)) - .then(k => this.serverless.cli.log('Finished Serverless Cloudflare-Worker deployment.')) + if (isMultiScript) { + return this.multiScriptDeployAll() + } else { + const functionObject = this.getFunctionObjectForSingleScript(); + return this.deploySingleScript(functionObject); + } + }) + .then(this.logDeployResponse) + .catch(error => { + return BB.reject(error.message); + }) + .then(k => this.serverless.cli.log(`Finished deployment in ${(Date.now() - startTime) / 1000} seconds.`)) + .then(k => this.serverless.cli.log('Finished Serverless Cloudflare-Worker deployment.')) }; } } diff --git a/deploy/cloudflareDeployFunction.js b/deploy/cloudflareDeployFunction.js index 24aa253..5d9cb7b 100644 --- a/deploy/cloudflareDeployFunction.js +++ b/deploy/cloudflareDeployFunction.js @@ -38,31 +38,34 @@ class CloudflareDeployFunction { this.hooks = { "deploy:function:deploy": () => - BB.bind(this) - .then(this.validateFunctionName) - .then( (functions) => { - functionNamesForDeploy = functions; - return this.checkAccountType; - }) - .then(async isMultiScript => { - if (isMultiScript && await duplicate.checkIfDuplicateRoutes(this.serverless, this.provider)) { - return BB.reject("Duplicate routes pointing to different script"); - } - - if (this.getInvalidScriptNames()) { - return BB.reject( - "Worker names can contain lowercase letters, numbers, underscores, and dashes. They cannot start with dashes." - ); - } - - if (isMultiScript) { - return this.multiScriptDeployAll(functionNamesForDeploy); - } else { - const functionObject = this.getFunctionObjectForSingleScript(); - return this.deploySingleScript(functionObject); - } - }) - .then(this.logDeployResponse) + BB.bind(this) + .then(this.validateFunctionName) + .then( (functions) => { + functionNamesForDeploy = functions; + return this.checkAccountType; + }) + .then(async isMultiScript => { + if (isMultiScript && await duplicate.checkIfDuplicateRoutes(this.serverless, this.provider)) { + return BB.reject("Duplicate routes pointing to different script"); + } + + if (this.getInvalidScriptNames()) { + return BB.reject( + "Worker names can contain lowercase letters, numbers, underscores, and dashes. They cannot start with dashes." + ); + } + + if (isMultiScript) { + return this.multiScriptDeployAll(functionNamesForDeploy); + } else { + const functionObject = this.getFunctionObjectForSingleScript(); + return this.deploySingleScript(functionObject); + } + }) + .then(this.logDeployResponse) + .catch(error => { + return BB.reject(error.message); + }) }; } } diff --git a/deploy/lib/logResponse.js b/deploy/lib/logResponse.js index d39c4a8..a6b5c7d 100644 --- a/deploy/lib/logResponse.js +++ b/deploy/lib/logResponse.js @@ -22,8 +22,8 @@ module.exports = { this.aggregateWorkerResponse(this.serverless.cli, workerScriptResponse); this.aggregateRoutesResponse(this.serverless.cli, routesResponse); } else { - this.parseWorkerResponse(this.serverless.cli, workerScriptResponse); - this.parseRoutesResponse(this.serverless.cli, routesResponse); + this.aggregateWorkerResponse(this.serverless.cli, [workerScriptResponse]); + this.aggregateRoutesResponse(this.serverless.cli, [routesResponse]); } } }; diff --git a/utils/index.js b/utils/index.js index 9d3d1fb..2c095f9 100644 --- a/utils/index.js +++ b/utils/index.js @@ -55,10 +55,16 @@ module.exports = { return { workerDeploySuccess, workerResult, workerErrors }; }, aggregateWorkerResponse(serverlessConsole, apiResponse) { - let status = []; - apiResponse.forEach(resp => { - status.push(this.parseWorkerResponse(serverlessConsole, resp)); - }); + let success = true; + const status = []; + for (let resp of apiResponse) { + const response = this.parseWorkerResponse(serverlessConsole, resp); + success &= response.workerDeploySuccess; + status.push(response); + } + if (!success) { + throw new Error('Worker script failed to deploy.'); + } return status; }, @@ -71,7 +77,8 @@ module.exports = { errors: routeErrors } = resp; - if (routeSuccess || !this.routeContainsFatalErrors(routeErrors)) { + routeSuccess |= !this.routeContainsFatalErrors(routeErrors); + if (routeSuccess) { serverlessConsole.log(`✅ Routes Deployed `); } else { serverlessConsole.log(`❌ Fatal Error, Routes Not Deployed!`); @@ -89,12 +96,18 @@ module.exports = { }, aggregateRoutesResponse(serverlessConsole, apiResponse) { - let status = []; - - apiResponse.forEach(resp => { - status.push(this.parseRoutesResponse(serverlessConsole, resp)); - }); - + let success = true; + const status = []; + for (const resp of apiResponse) { + const routesResults = this.parseRoutesResponse(serverlessConsole, resp); + for (const routeResult of routesResults) { + success &= routeResult.routesSuccess; + } + status.push(routesResults); + } + if (!success) { + throw new Error('Worker routes failed to deploy.'); + } return status; },