From 99ac19a12f7312b0435d5a87596a4a15b5f56fca Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Thu, 11 Oct 2018 18:04:13 +0300 Subject: [PATCH] Throw better errors for failing (not missing) plugins (#243) --- index.js | 5 ++++- test/error.js | 15 ++++++++++++++- test/fixtures/bad-plugin.js | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/bad-plugin.js diff --git a/index.js b/index.js index eed5d3e..9501e42 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,10 @@ const cliConfig = { try { return require(plugin)() } catch (e) { - return error(`Plugin Error: Cannot find module '${plugin}'`) + const msg = e.message || `Cannot find module '${plugin}'` + let prefix = msg.includes(plugin) ? '' : ` (${plugin})` + if (e.name && e.name !== 'Error') prefix += `: ${e.name}` + return error(`Plugin Error${prefix}: ${msg}'`) } }) : [] diff --git a/test/error.js b/test/error.js index b49781b..3d7732f 100644 --- a/test/error.js +++ b/test/error.js @@ -40,7 +40,7 @@ test.failing('invalid --config', t => { }) }) -test('PluginError', t => { +test('plugin not found', t => { return cli(['test/fixtures/a.css', '-u', 'postcss-plugin', '-o', tmp()]).then( ({ err, code }) => { t.is(code, 1, 'expected non-zero error code') @@ -52,6 +52,19 @@ test('PluginError', t => { ) }) +test('plugin throws on require', t => { + return cli([ + 'test/fixtures/a.css', + '-u', + './test/fixtures/bad-plugin', + '-o', + tmp() + ]).then(({ err, code }) => { + t.is(code, 1, 'expected non-zero error code') + t.regex(err.toString(), /Plugin Error \(.*bad-plugin\): This fails/) + }) +}) + test('CssSyntaxError', t => { return cli(['test/fixtures/a.css', '--parser', 'sugarss', '-o', tmp()]).then( ({ err, code }) => { diff --git a/test/fixtures/bad-plugin.js b/test/fixtures/bad-plugin.js new file mode 100644 index 0000000..2911e95 --- /dev/null +++ b/test/fixtures/bad-plugin.js @@ -0,0 +1 @@ +throw new Error('This fails')