Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Include filename in error message for compile errors #40

Open
wants to merge 1 commit into from

3 participants

@pselden

For: #38

Appends the filename to the Error message when there's an error during template compilation.

@ForbesLindesay

Shouldn't this be:

if (typeof filename != 'undefined') {
@ForbesLindesay

+1 I keep running into this all the time, I really want this fixed.

@tj
Owner
tj commented

uhh why are you using a global filename

@ForbesLindesay

Speaking of which I was wrong in my earlier about needing to be typeof, I hadn't read it carefully enough.

@tj tj commented on the diff
test/ejs.test.js
@@ -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 };
+
@tj Owner
tj added a note

^ var, no need for globals here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tj tj commented on the diff
test/ejs.test.js
@@ -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);
@tj Owner
tj added a note

ejs.compile(str, { filename: 'fake.js' }) is fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 12, 2012
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 1 deletion.
  1. +10 −1 lib/ejs.js
  2. +16 −0 test/ejs.test.js
View
11 lib/ejs.js
@@ -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') {
+ e.message += ' in ' + filename;
+ }
+
+ throw e;
+ }
+
return function(locals){
return fn.call(this, locals, filters, utils.escape);
}
View
16 test/ejs.test.js
@@ -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 };
+
@tj Owner
tj added a note

^ var, no need for globals here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ try {
+ ejs.compile(str, options);
@tj Owner
tj added a note

ejs.compile(str, { filename: 'fake.js' }) is fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ } catch (err) {
+ assert.ok(~err.message.indexOf(filename));
+ }
}
};
Something went wrong with that request. Please try again.