I'm wondering what the best way to handle errors in long functions with promises are?
My function:
module.exports = function(data) {
  var deferred = Q.defer();
  var config = {};
  fs.readdir(path, function(err, files) {
    if (err) {
      deferred.reject(new Error(err));
    }
    var infoPath = false;
    files.forEach(function(filename) {
      if (filename.indexOf('.info') !== -1) {
        infoPath = path + '/' + filename;
        config['moduleName'] = filename.replace('.info', '');
      }
    });
    if (!infoPath) {
      deferred.reject(new Error('Did not find .info-file'));
    }
    if (files.indexOf(config['moduleName'] + '.module') > -1) {
      config.type = 'Modules';
    }
    else {
      deferred.reject(new Error('Unknown project type'));
    }
    // Parse the info file.
    fs.readFile(infoPath, function (err, content) {
      if (err) {
        deferred.reject(new Error(err));
      }
      data.config = config;
      data.infoContent = content.toString();
      deferred.resolve(data); 
    });
  });
  return deferred.promise;
};
As far as I understand it this is the way to use Q.defer. But if a error is thrown, I don't want/need it to try the rest of function. Am I missing something or are there a better way to do this?
 
    