Permalink
Browse files

`swig.fromFile` renders a nice error when allowErrors is off. closes …

…gh-9
  • Loading branch information...
1 parent 395e02c commit 45df0cc6e175364ec2a342631edc7a8119297105 @paularmstrong committed Sep 27, 2011
Showing with 40 additions and 5 deletions.
  1. +13 −5 index.js
  2. +26 −0 tests/index.test.js
  3. +1 −0 tests/templates/includes_notfound.html
View
@@ -106,13 +106,21 @@ exports.fromFile = function (filepath) {
return CACHE[filepath];
}
- var data = fs.readFileSync(config.root + '/' + filepath, config.encoding);
-
- // TODO: see what error readFileSync returns and warn about it
- if (data) {
+ var get = function () {
+ var data = fs.readFileSync(config.root + '/' + filepath, config.encoding);
CACHE[filepath] = createTemplate(data, filepath);
- return CACHE[filepath];
+ };
+
+ if (config.allowErrors) {
+ get();
+ } else {
+ try {
+ get();
+ } catch (error) {
+ CACHE[filepath] = new TemplateError(error);
+ }
}
+ return CACHE[filepath];
};
exports.fromString = function (string) {
View
@@ -0,0 +1,26 @@
+var swig = require('../index'),
+ testCase = require('nodeunit').testCase;
+
+exports.fromFile = testCase({
+ setUp: function (callback) {
+ callback();
+ },
+
+ 'file not exist': function (test) {
+ swig.init({
+ root: __dirname + '/templates',
+ allowErrors: false
+ });
+ var tpl = swig.fromFile('foobar.html');
+ test.ok((/<pre>Error\: EBADF, Bad file descriptor/).test(tpl.render()), 'pushes a render function with the error');
+ tpl = swig.fromFile('includes_notfound.html');
+ test.ok((/<pre>Error\: EBADF, Bad file descriptor/).test(tpl.render()), 'pushes a render function with the error');
+
+ swig.init({ allowErrors: true });
+ test.throws(function () {
+ tpl = swig.fromFile('barfoo.html');
+ }, 'throws when allowErrors is true');
+
+ test.done();
+ }
+});
@@ -0,0 +1 @@
+{% include 'foobar.html' %}

0 comments on commit 45df0cc

Please sign in to comment.