[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
*
*/
var fs = require('fs'),
util = require('util'),
var fs = require('fs'),
path = require('path'),
util = require('util'),
Memory = require('./memory').Memory;
//
@ -60,27 +61,15 @@ File.prototype.save = function (value, callback) {
// Saves the current configuration object to disk at `this.file`
// using the format specified by `this.format` synchronously.
//
File.prototype.saveSync = function (value, callback) {
if (!callback) {
callback = value;
value = null;
}
File.prototype.saveSync = function (value) {
var err;
try {
fs.writeFileSync(this.file, this.format.stringify(this.store));
}
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) {
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.
// Attempts to load the data stored in `this.file` synchronously and responds appropriately.
//
File.prototype.loadSync = function (callback) {
var err, data;
try {
data = fs.readFileSync(this.file, 'utf8');
this.store = this.format.parse(data);
File.prototype.loadSync = function () {
var data, self = this;
if (!path.existsSync(self.file)) {
// If the path we are attempting to load doesn't exist, create it
self.saveSync({});
self.store = {};
data = {};
}
catch (ex) {
err = ex;
else {
// 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;
};