renderFile may call callback twice if it throws an error #227

Closed
awwright opened this Issue May 24, 2011 · 0 comments

2 participants

@awwright

In exports.renderFile in lib/index.js, if the requested file is not cached, it will be rendered with the following code:

    fs.readFile(path, 'utf8', function(err, str){
      if (err) return fn(err);
      try {
        fn(null, exports.render(str, options));
      } catch (err) {
        fn(err);
      }
    });

This poses a problem because if the call to fn() creates an exception (or any error) it wil be caught by Jade and be called again in the catch block. Callbacks are of course only supposed to be called once, and Jade shouldn't be catching outside exceptions anyways.
The code should probably look something like this:

    fs.readFile(path, 'utf8', function(err, str){
      if (err) return fn(err);
      try {
        var result = exports.render(str, options);
      } catch (err) {
        return fn(err);
      }
      fn(null, result);
    });
@tj tj closed this in 29b6610 May 24, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment