'use strict';

var util = require('util');
var path = require('path');
var chalk = require('chalk');
var gutil = require('gulp-util');
var through = require('through2');
var extend = require('xtend');

module.exports = function(name, callback, defaults) {

  return function(options) {

    var config = extend(defaults, options);

    function transform(file, encoding, next) {

      var emit = this.emit;
      function error(err) {
        emit('error', new gutil.PluginError(name, err));
      }

      if(file.isNull()) {
        return next(null, file); // pass along
      }

      try {
        callback.call(this, file, config);
      } catch(err) {
        return error(err);
      }

      if(config.debug) {
        gutil.log(util.format('Processed \'%s\' through %s', chalk.cyan(path.relative(process.cwd(), file.path)), chalk.magenta(name)));
      }

      next(null, file);

    }

    return through.obj(transform);

  }

};

@@ -629,16 +629,296 @@
 , config
+, function(err) %7B%0A          // Gracefully recover from an error%0A          return gutil.log(util.format('Encountered the following error while processing %5C'%25s%5C' through %25s%5Cn%25s', chalk.cyan(path.relative(process.cwd(), file.path)), chalk.magenta('jade'), chalk.red(err)));%0A        %7D
 );%0A     

allow graceful error handling