Skip to content

Commit

Permalink
Merge pull request #101 from gucong3000/error_details
Browse files Browse the repository at this point in the history
Error details
  • Loading branch information
w0rm authored Mar 20, 2017
2 parents e572833 + f7535b8 commit 343d410
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ module.exports = withConfigLoader(function (loadConfig) {
function handleError (error) {
var errorOptions = { fileName: file.path, showStack: true }
if (error.name === 'CssSyntaxError') {
error = error.message + '\n\n' + error.showSourceCode() + '\n'
errorOptions.error = error
errorOptions.fileName = error.file || file.path
errorOptions.lineNumber = error.line
errorOptions.showProperties = false
errorOptions.showStack = false
error = error.message + '\n\n' + error.showSourceCode() + '\n'
}
// Prevent stream’s unhandled exception from
// being suppressed by Promise
Expand Down
32 changes: 23 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ it('should correctly wrap postcss errors', function (cb) {
stream.on('error', function (err) {
assert.ok(err instanceof gutil.PluginError)
assert.equal(err.plugin, 'gulp-postcss')
assert.equal(err.column, 1)
assert.equal(err.lineNumber, 1)
assert.equal(err.name, 'CssSyntaxError')
assert.equal(err.reason, 'Unclosed block')
assert.equal(err.showStack, false)
assert.equal(err.fileName, 'testpath')
assert.equal(err.source, 'a {')
assert.equal(err.fileName, path.resolve('testpath'))
cb()
})

stream.write(new gutil.File({
contents: new Buffer('a {'),
path: 'testpath'
path: path.resolve('testpath')
}))

stream.end()
Expand All @@ -75,14 +80,15 @@ it('should respond with error on stream files', function (cb) {
assert.ok(err instanceof gutil.PluginError)
assert.equal(err.plugin, 'gulp-postcss')
assert.equal(err.showStack, true)
assert.equal(err.fileName, 'testpath')
assert.equal(err.message, 'Streams are not supported!')
assert.equal(err.fileName, path.resolve('testpath'))
cb()
})

var streamFile = {
isStream: function () { return true },
isNull: function() { return false },
path: 'testpath'
path: path.resolve('testpath')
};

stream.write(streamFile)
Expand Down Expand Up @@ -149,12 +155,19 @@ it('should correctly generate relative source map', function (cb) {
describe('PostCSS Guidelines', function () {

var sandbox = sinon.sandbox.create()
var CssSyntaxError = function (message, sourceCode) {
var CssSyntaxError = function (message, source) {
this.name = 'CssSyntaxError'
this.message = message
this.sourceCode = sourceCode
this.source = source
this.showSourceCode = function () {
return this.sourceCode
return this.source
}
this.toString = function(){
var code = this.showSourceCode();
if ( code ) {
code = '\n\n' + code + '\n';
}
return this.name + ': ' + this.message + code;
}
}
var postcssStub = {
Expand Down Expand Up @@ -404,12 +417,13 @@ describe('PostCSS Guidelines', function () {
it('should not output js stack trace for `CssSyntaxError`', function (cb) {

var stream = postcss([ doubler ])
var cssSyntaxError = new CssSyntaxError('message', 'sourceCode')
var cssSyntaxError = new CssSyntaxError('messageText', 'sourceCode')
postcssStub.process.returns(Promise.reject(cssSyntaxError))

stream.on('error', function (error) {
assert.equal(error.showStack, false)
assert.equal(error.message, 'message' + '\n\nsourceCode\n')
assert.equal(error.message, 'messageText\n\nsourceCode\n')
assert.equal(error.source, 'sourceCode')
cb()
})

Expand Down

0 comments on commit 343d410

Please sign in to comment.