From 6715feebe3bf9da8ebb28d70346868e960ffc8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=BC=E6=99=82=E5=BC=A6=E4=B9=9F?= Date: Sat, 22 Feb 2014 11:20:31 +0800 Subject: [PATCH] Update# add 'not found handler' for include feature to prevent gulp interrupt by plugin error --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index 175128e..33d2227 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,16 @@ var es = require('event-stream'); var gutil = require('gulp-util'); var ejs = require('ejs'); +var logPrefix = "[" + gutil.colors.blue("ejs") + "]"; + +var notFoundHandler = function(err, file, settings) { + var errorMessage = "partial '" + err.path + "' not found"; + gutil.log(logPrefix, gutil.colors.red(errorMessage)); + file.contents = new Buffer(errorMessage); // Output error message into file + file.path = gutil.replaceExtension(file.path, settings.ext); + return file; +} + module.exports = function (options, settings) { settings = settings || {}; options = options || {}; @@ -15,6 +25,10 @@ module.exports = function (options, settings) { file.contents = new Buffer(ejs.render(file.contents.toString(), options)); file.path = gutil.replaceExtension(file.path, settings.ext); } catch (err) { + if(err.code === 'ENOENT') { + // Partial file not found, keep gulp and log error + return cb(null, notFoundHandler(err, file, settings)); + } return cb(new gutil.PluginError('gulp-ejs', err)); } cb(null, file);