Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw better errors for failing (not missing) plugins #243

Merged
merged 1 commit into from Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')