Skip to content

Commit 99ac19a

Browse files
eemeliRyanZim
authored andcommitted
Throw better errors for failing (not missing) plugins (#243)
1 parent 85d4cc9 commit 99ac19a

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ const cliConfig = {
3434
try {
3535
return require(plugin)()
3636
} catch (e) {
37-
return error(`Plugin Error: Cannot find module '${plugin}'`)
37+
const msg = e.message || `Cannot find module '${plugin}'`
38+
let prefix = msg.includes(plugin) ? '' : ` (${plugin})`
39+
if (e.name && e.name !== 'Error') prefix += `: ${e.name}`
40+
return error(`Plugin Error${prefix}: ${msg}'`)
3841
}
3942
})
4043
: []

test/error.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test.failing('invalid --config', t => {
4040
})
4141
})
4242

43-
test('PluginError', t => {
43+
test('plugin not found', t => {
4444
return cli(['test/fixtures/a.css', '-u', 'postcss-plugin', '-o', tmp()]).then(
4545
({ err, code }) => {
4646
t.is(code, 1, 'expected non-zero error code')
@@ -52,6 +52,19 @@ test('PluginError', t => {
5252
)
5353
})
5454

55+
test('plugin throws on require', t => {
56+
return cli([
57+
'test/fixtures/a.css',
58+
'-u',
59+
'./test/fixtures/bad-plugin',
60+
'-o',
61+
tmp()
62+
]).then(({ err, code }) => {
63+
t.is(code, 1, 'expected non-zero error code')
64+
t.regex(err.toString(), /Plugin Error \(.*bad-plugin\): This fails/)
65+
})
66+
})
67+
5568
test('CssSyntaxError', t => {
5669
return cli(['test/fixtures/a.css', '--parser', 'sugarss', '-o', tmp()]).then(
5770
({ err, code }) => {

test/fixtures/bad-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('This fails')

0 commit comments

Comments
 (0)