From 77f0e3228da24657af1cf74d1d3a4018ee7fd078 Mon Sep 17 00:00:00 2001 From: Anton Drukh Date: Sat, 8 Sep 2018 14:45:46 +0300 Subject: [PATCH] fix: throw proper error on failing gradle execution --- lib/index.js | 19 +++++++++++++------ lib/sub-process.js | 2 +- test/system/plugin.test.js | 16 +++++++++++++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index a5f0d21..3ddf38a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,7 +18,9 @@ function inspect(root, targetFile, options) { if (!options) { options = {dev: false}; } - return getPackage(root, targetFile, options) + var command = getCommand(root, targetFile); + var args = buildArgs(root, targetFile, options.args); + return getPackage(root, command, args) .then(function (pkg) { // opt-in with `jars` or `localjars` flag if (options.jars || options.localjars) { @@ -40,14 +42,19 @@ function inspect(root, targetFile, options) { }, package: pkg, }; + }) + .catch(function (error) { + error.message = error.message + '\n\n' + + 'Please make sure that `' + command + ' ' + args.join(' ') + + '` executes successfully on this project.\n\n' + + 'If the problem persists, collect the output of `' + + command + ' ' + args.join(' ') + '` and contact support@snyk.io\n'; + throw error; }); } -function getPackage(root, targetFile, options) { - return subProcess.execute( - getCommand(root, targetFile), - buildArgs(root, targetFile, options.args), - {cwd: root}) +function getPackage(root, command, args) { + return subProcess.execute(command, args, {cwd: root}) .then(function (result) { var packageName = path.basename(root); var packageVersion = '0.0.0'; diff --git a/lib/sub-process.js b/lib/sub-process.js index 6e35326..1017376 100644 --- a/lib/sub-process.js +++ b/lib/sub-process.js @@ -20,7 +20,7 @@ module.exports.execute = function (command, args, options) { proc.on('close', function (code) { if (code !== 0) { - return reject(stdout || stderr); + return reject(new Error(stdout || stderr)); } resolve(stdout || stderr); }); diff --git a/test/system/plugin.test.js b/test/system/plugin.test.js index 595dccf..0cbb1fb 100644 --- a/test/system/plugin.test.js +++ b/test/system/plugin.test.js @@ -31,6 +31,20 @@ test('run inspect()', function (t) { .catch(t.fail); }); +test('failing inspect()', function (t) { + t.plan(1); + stubSubProcessExec(t); + return plugin.inspect('.', path.join( + __dirname, '..', 'fixtures', 'no-wrapper', 'build.gradle')) + .then(function (result) { + t.fail('Should have thrown!', result); + }) + .catch(function (error) { + t.match(error.message, 'executes successfully on this project', + 'proper error message'); + }); +}); + test('windows without wrapper', function (t) { t.plan(1); @@ -134,7 +148,7 @@ function stubPlatform(platform, t) { function stubSubProcessExec(t) { sinon.stub(subProcess, 'execute') .callsFake(function () { - return Promise.reject('abort'); + return Promise.reject(new Error('abort')); }); t.teardown(subProcess.execute.restore); }