[refactor]: Cleaned up error handling on File.loadSync and File.load

[refactor]: Using path module to determine if file exists instead of throwing error
[api]: File.load and File.loadSync will now automatically create the requested JSON file path if no file is found.
This commit is contained in:
Marak Squires 2011-06-23 18:06:26 -07:00
parent d8627a9475
commit c43685160d

View file

@ -4,9 +4,10 @@
* (C) 2011, Charlie Robbins * (C) 2011, Charlie Robbins
* *
*/ */
var fs = require('fs'), var fs = require('fs'),
util = require('util'), path = require('path'),
util = require('util'),
Memory = require('./memory').Memory; Memory = require('./memory').Memory;
// //
@ -60,27 +61,15 @@ File.prototype.save = function (value, callback) {
// Saves the current configuration object to disk at `this.file` // Saves the current configuration object to disk at `this.file`
// using the format specified by `this.format` synchronously. // using the format specified by `this.format` synchronously.
// //
File.prototype.saveSync = function (value, callback) { File.prototype.saveSync = function (value) {
if (!callback) {
callback = value;
value = null;
}
var err;
try { try {
fs.writeFileSync(this.file, this.format.stringify(this.store)); fs.writeFileSync(this.file, this.format.stringify(this.store));
} }
catch (ex) { catch (ex) {
err = ex; throw(ex);
} }
if (callback) {
return callback(err);
}
if (err) {
throw err;
}
}; };
// //
@ -90,21 +79,37 @@ File.prototype.saveSync = function (value, callback) {
// //
File.prototype.load = function (callback) { File.prototype.load = function (callback) {
var self = this; var self = this;
fs.readFile(this.file, function (err, data) {
if (err) {
return callback(err);
}
try {
self.store = self.format.parse(data.toString());
}
catch (ex) {
self.store = {};
return callback(ex);
}
callback(null, self.store);
path.exists(self.file, function(exists){
if (!exists) {
// If the path we are attempting to load doesn't exist, create it
self.save({}, function(err){
self.store = {};
return callback(err, self.store);
});
}
else {
// Else, the path exists, read it from disk
fs.readFile(self.file, function (err, data) {
if (err) {
return callback(err);
}
try {
self.store = self.format.parse(data.toString());
}
catch (ex) {
return callback(new Error("Error parsing your JSON configuration file."));
}
callback(null, self.store);
});
}
}); });
}; };
// //
@ -112,25 +117,29 @@ File.prototype.load = function (callback) {
// #### @callback {function} **Optional** Continuation to respond to when complete. // #### @callback {function} **Optional** Continuation to respond to when complete.
// Attempts to load the data stored in `this.file` synchronously and responds appropriately. // Attempts to load the data stored in `this.file` synchronously and responds appropriately.
// //
File.prototype.loadSync = function (callback) { File.prototype.loadSync = function () {
var err, data; var data, self = this;
try { if (!path.existsSync(self.file)) {
data = fs.readFileSync(this.file, 'utf8');
this.store = this.format.parse(data); // If the path we are attempting to load doesn't exist, create it
self.saveSync({});
self.store = {};
data = {};
} }
catch (ex) { else {
err = ex;
// Else, the path exists, read it from disk
try {
data = fs.readFileSync(this.file, 'utf8');
this.store = this.format.parse(data);
}
catch (ex) {
throw new Error("Error parsing your JSON configuration file.")
}
} }
if (callback) {
return callback(err, data);
}
if (err) {
err.message = "Error parsing your JSON configuration file."
throw err;
}
return data; return data;
}; };