Skip to content

Commit

Permalink
Making error handling more configurable, catching errors more effecti…
Browse files Browse the repository at this point in the history
…vely.
  • Loading branch information
tizzo committed Nov 5, 2014
1 parent b377e63 commit a600a92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ var async = require('async');
var util = require('util');
var EventEmitter = require('events').EventEmitter;

var Loader = function() {
var Loader = function(params) {
params = params || {};
this.load = this.load.bind(this);
this.parseYaml = this.parseYaml.bind(this);
this.add = this.add.bind(this);
this.errorHandler = this.errorHandler.bind(this);
this.context = {};
this.loads = [];
this.loads = [];
this.loads = [];
this.errorOnError = false;
this.fastError = params.fastError;
};
util.inherits(Loader, EventEmitter);

Expand All @@ -25,11 +24,11 @@ util.inherits(Loader, EventEmitter);
Loader.prototype.errorHandler = function(error, done) {
arguments = Array.prototype.slice.call(arguments, 0);
var done = arguments.pop();
this.emit('error', error);
if (error && this.errorOnError) {
if (error && this.fastError) {
done(error);
}
else {
this.emit('error', error);
done(null, {});
}
};
Expand Down Expand Up @@ -151,7 +150,7 @@ Loader.prototype.loadFile = function(path, options, done) {
if (exists) {
fs.readFile(path, 'utf8', function(error, data) {
/* istanbul ignore if: This error condition is near impossible to test. */
if (error) return done(error);
if (error) return self.errorHandler(error, done);
self.parseYaml(data, done);
});
}
Expand All @@ -168,13 +167,13 @@ Loader.prototype.loadDirectory = function(dirPath, options, done) {
var self = this;
fs.readdir(dirPath, function(error, files) {
/* istanbul ignore if: This error condition is near impossible to test. */
if (error) return done(error);
if (error) return self.errorHandler(error, done);
var loadFile = function(filePath, cb) {
self.loadFile(path.join(dirPath, filePath), options, cb);
};
async.map(files, loadFile, function(error, confs) {
/* istanbul ignore if: This error condition is near impossible to test. */
if (error) return done(error);
if (error) return self.errorHandler(error, done);
var conf = {};
for (i in confs) {
conf = self.mergeConifguration(conf, confs[i]);
Expand All @@ -200,15 +199,15 @@ Loader.prototype.loadDirectoryArray = function(dirPath, configKey, options, done
var self = this;
fs.readdir(dirPath, function(error, files) {
/* istanbul ignore if: This error condition is near impossible to test. */
if (error) return done(error);
if (error) return self.errorHandler(error, done);
var output = {};
output[configKey] = [];
var fileLoadHandler = function(file, cb){
fs.readFile(path.join(dirPath, file), 'utf8', cb);
};
async.map(files, fileLoadHandler, function(error, confs) {
/* istanbul ignore if: This error condition is near impossible to test. */
if (error) return done(error);
if (error) return self.errorHandler(error, done);
for (i in confs) {
try {
var conf = yaml.safeLoad(confs[i]);
Expand Down
2 changes: 1 addition & 1 deletion test/directoryArrayLoaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('yaml-config-loader', function() {
});
describe('addDirectoryArray', function() {
it('should throw an error if a non-existant directory is specified', function(done) {
var loader = new Loader();
var loader = new Loader({fastError: true});
loader.addDirectoryArray(fixture('nonsense'));
loader.load(function(error, config) {
should.exist(error);
Expand Down

0 comments on commit a600a92

Please sign in to comment.