Skip to content

Commit

Permalink
Improve emitting errors and show stack
Browse files Browse the repository at this point in the history
- rename Error to PluginError to not shadow actual Error
- consistenly use PluginError
- handle posthtml-expressions errors by not passing the error object
directly
- show stack which is very useful for plugins like this

posthtml/posthtml-expressions#89 (comment)
  • Loading branch information
silvenon committed Jun 25, 2020
1 parent 79483f4 commit 253a944
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const PLUGIN_NAME = 'gulp-posthtml'
const posthtml = require('posthtml')
const posthtmlrc = require('posthtml-load-config')

const Error = require('plugin-error')
const PluginError = require('plugin-error')

function rc (cb) {
return function (plugins, options) {
Expand Down Expand Up @@ -67,7 +67,12 @@ module.exports = rc((loadConfig) => {
}

if (file.isStream()) {
return new Error(`Streams are not supported by ${PLUGIN_NAME}`)
return cb(
new PluginError({
plugin: PLUGIN_NAME,
message: 'Streams are not supported'
})
)
}

loadConfig(file).then((config) => {
Expand All @@ -82,7 +87,17 @@ module.exports = rc((loadConfig) => {
cb(null, file)
})
.catch((err) => {
cb(new Error(PLUGIN_NAME, err))
// passing the error object directly would usually be fine,
// but plugins like posthtml-expressions are an exception, so we're being safe
// https://github.com/posthtml/posthtml-expressions/issues/89
cb(
new PluginError({
plugin: PLUGIN_NAME,
message: err.message,
stack: err.stack,
showStack: true
})
)
})
})
})
Expand Down

0 comments on commit 253a944

Please sign in to comment.