tj / ejs

Embedded JavaScript templates for node
4.47k stars 513 forks source link

Better error reporting for syntax errors #38

Open pselden opened 12 years ago

pselden commented 12 years ago

The error reporting that ejs does have is really nice, and makes it easy to track down things like undefined variables and so forth.

However, it is lacking when It comes to reporting syntax errors:

<% if(true) {
        <p>Uh oh... forgot to close the statement!</p>
<% } %>

SyntaxError: Unexpected token <
    at Object.Function (unknown source)
    at Object.compile (/Users/pselden/Documents/express-test/node_modules/ejs/lib/ejs.js:199:12)
    at Function.compile (/Users/pselden/Documents/express-test/node_modules/express/lib/view.js:65:33)

Is there any way to improve upon those types of errors and possibly report the line number where it failed on?

pselden commented 12 years ago

I looked into this and realized that it's just passing it to the Function method for syntax parsing, so the line numbers might be tricky or impossible to add with the current approach (unless it is done in the parse method).

However, it would be useful to add some additional information, like the filename where it failed to the error message. Would it be possible to catch the exception and do something simple like: e.message += ' in ' + filename; and then rethrow it?

tj commented 12 years ago

not much we can do as far as line numbers go but yeah we could improve it