Permalink
Browse files

started .compile() error handling

  • Loading branch information...
1 parent 8a54850 commit c7cd1ef3bc53955d569687be2de54a7d558d3ded @tj tj committed Nov 20, 2010
Showing with 25 additions and 2 deletions.
  1. +14 −2 lib/jade.js
  2. +11 −0 test/jade.test.js
View
@@ -203,10 +203,22 @@ function parse(str, options){
*/
exports.compile = function(str, options){
+ var options = options || {},
+ input = JSON.stringify(str),
+ filename = options.filename
+ ? JSON.stringify(options.filename)
+ : 'undefined';
+
var fn = [
- 'var _ = { lineno: 1 };',
- parse(String(str), options || {})
+ 'var _ = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };',
+ rethrow.toString(),
+ 'try {',
+ parse(String(str), options || {}),
+ '} catch (err) {',
+ ' rethrow(err, _.input, _.filename, _.lineno);',
+ '}'
].join('\n');
+
return new Function('locals', fn);
};
View
@@ -852,5 +852,16 @@ module.exports = {
'test .compile() scope': function(assert){
var fn = jade.compile('p= this.foo');
assert.equal('<p>bar</p>', fn.call({ foo: 'bar' }));
+ },
+
+ 'test .compile() error handling': function(assert){
+ var fn = jade.compile('p= asdf'),
+ err;
+ try {
+ fn();
+ } catch (e) {
+ err = e;
+ }
+ console.log(err.stack);
}
};

0 comments on commit c7cd1ef

Please sign in to comment.