Skip to content

Commit

Permalink
Improved error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonmoeller committed Apr 20, 2015
1 parent 3c10385 commit 3559a63
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
7 changes: 7 additions & 0 deletions gulpfile.js
@@ -1,6 +1,7 @@
'use strict';

var gulp = require('gulp'),
plumber = require('gulp-plumber'),
paths = {
gulp: './gulpfile.js',
src: './index.js',
Expand All @@ -15,6 +16,7 @@ gulp.task('lint', function () {

return gulp
.src([paths.gulp, paths.src, paths.test])
.pipe(plumber())
.pipe(jscs())
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
Expand All @@ -35,6 +37,11 @@ gulp.task('test', ['lint', 'cover'], function () {

return gulp
.src(paths.test)
.pipe(plumber())
.pipe(mocha({ reporter: 'spec' }))
.pipe(istanbul.writeReports());
});

gulp.task('watch', function () {
gulp.watch([paths.src, paths.test], ['test']);
});
42 changes: 24 additions & 18 deletions index.js
@@ -1,6 +1,7 @@
'use strict';

var hb = require('handlebars'),
var gutil = require('gulp-util'),
hb = require('handlebars'),
logger = require('./util/logger'),
registrar = require('handlebars-registrar'),
requireGlob = require('require-glob'),
Expand Down Expand Up @@ -58,27 +59,32 @@ module.exports = function (options) {

// Stream it. Stream it good.
return through.obj(function (file, enc, cb) {
var context = Object.create(data || {}),
template = hb.compile(file.contents.toString());
try {
var context = Object.create(data || {}),
template = hb.compile(file.contents.toString());

if (includeFile) {
context.file = file;
}
if (includeFile) {
context.file = file;
}

if (typeof options.dataEach === 'function') {
context = options.dataEach(context, file);
}
if (typeof options.dataEach === 'function') {
context = options.dataEach(context, file);
}

if (options.debug) {
logger.file(file.path.replace(file.base, ''));
logger.keys(' data', data);
logger.keys(' context', context);
logger.keys(' helpers', hb.helpers);
logger.keys(' partials', hb.partials);
}
if (options.debug) {
logger.file(file.path.replace(file.base, ''));
logger.keys(' data', data);
logger.keys(' context', context);
logger.keys(' helpers', hb.helpers);
logger.keys(' partials', hb.partials);
}

file.contents = new Buffer(template(context));
file.contents = new Buffer(template(context));

cb(null, file);
cb(null, file);
}
catch (err) {
cb(new gutil.PluginError('gulp-hb', err));
}
});
};
3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "gulp-hb",
"version": "2.4.1",
"version": "2.4.2",
"description": "A sane Handlebars Gulp plugin.",
"keywords": [
"gulpplugin",
Expand Down Expand Up @@ -46,6 +46,7 @@
"gulp-jscs": "^1.5.2",
"gulp-jshint": "^1.10.0",
"gulp-mocha": "^2.0.1",
"gulp-plumber": "^1.0.0",
"jshint-stylish": "^1.0.1",
"vinyl-fs": "^1.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/templates/error.html
@@ -0,0 +1 @@
{{>>> derp}}
12 changes: 11 additions & 1 deletion test/gulp-hb.e2e.js
Expand Up @@ -23,11 +23,17 @@ describe('gulp-hb e2e', function () {
done();
}

function expectError(err) {
expect(err.plugin).toBe('gulp-hb');
expect(err.message).toContain('derp');
done();
}

vinylFs
.src(fixture)
.pipe(plugin)
.on('data', expectFile)
.on('error', done);
.on('error', expectError);
}

beforeEach(function () {
Expand Down Expand Up @@ -134,4 +140,8 @@ describe('gulp-hb e2e', function () {

testWithFile('dataEach.html', hb(options), done);
});

it('should handle errors', function (done) {
testWithFile('error.html', hb(), done);
});
});

0 comments on commit 3559a63

Please sign in to comment.