Skip to content

Commit

Permalink
Throw better errors for failing (not missing) plugins (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli authored and RyanZim committed Oct 11, 2018
1 parent 85d4cc9 commit 99ac19a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -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}'`)
}
})
: []
Expand Down
15 changes: 14 additions & 1 deletion test/error.js
Expand Up @@ -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')
Expand All @@ -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 }) => {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/bad-plugin.js
@@ -0,0 +1 @@
throw new Error('This fails')

0 comments on commit 99ac19a

Please sign in to comment.