Permalink
Browse files

Append filename to error message if there's an error during compile.

  • Loading branch information...
1 parent eb66597 commit 93cb002df16efe23a481a22ba27c04f03a0a60e4 Paul Selden committed Jan 12, 2012
Showing with 26 additions and 1 deletion.
  1. +10 −1 lib/ejs.js
  2. +16 −0 test/ejs.test.js
View
@@ -196,7 +196,16 @@ var compile = exports.compile = function(str, options){
].join("\n");
if (options.debug) console.log(str);
- var fn = new Function('locals, filters, escape', str);
+ try {
+ var fn = new Function('locals, filters, escape', str);
+ } catch(e) {
+ if(filename != 'undefined') {
@ForbesLindesay
ForbesLindesay Sep 17, 2012

Shouldn't this be:

if (typeof filename != 'undefined') {
+ e.message += ' in ' + filename;
+ }
+
+ throw e;
+ }
+
return function(locals){
return fn.call(this, locals, filters, utils.escape);
}
View
@@ -286,5 +286,21 @@ module.exports = {
var lineno = parseInt(err.toString().match(/ejs:(\d+)\n/)[1]);
assert.deepEqual(lineno, 6, "Error should been thrown on line 3, was thrown on line "+lineno);
}
+ },
+
+ 'test compile error reports filename': function(){
+ var str = [
+ '<% if(true) {',
+ '<p>Forgot to close the tag</p>',
+ '<% } %>'
+ ].join('\n'),
+ filename = 'test/fakefile.ejs',
+ options = { filename: filename };
+
+ try {
+ ejs.compile(str, options);
+ } catch (err) {
+ assert.ok(~err.message.indexOf(filename));
+ }
}
};

0 comments on commit 93cb002

Please sign in to comment.