remove broken cache files if found, fixes #27

This commit is contained in:
Dave Eddy 2015-09-21 17:00:58 -04:00
parent 44ce942d97
commit 7ab6453b1f

View File

@ -145,27 +145,32 @@ TritonApi.prototype._cacheGetJson = function _cacheGetJson(key, cb) {
assert.func(cb, 'cb'); assert.func(cb, 'cb');
var keyPath = path.resolve(this.cacheDir, key); var keyPath = path.resolve(this.cacheDir, key);
fs.exists(keyPath, function (exists) { fs.readFile(keyPath, 'utf8', function (err, data) {
if (!exists) { if (err && err.code === 'ENOENT') {
self.log.trace({keyPath: keyPath}, 'cache file does not exist'); self.log.trace({keyPath: keyPath},
'cache file does not exist');
return cb();
} else if (err) {
self.log.warn({err: err, keyPath: keyPath},
'error reading cache file');
return cb(); return cb();
} }
fs.readFile(keyPath, 'utf8', function (err, data) { var obj;
if (err) { try {
self.log.warn({err: err, keyPath: keyPath}, obj = JSON.parse(data);
'error reading cache file'); } catch (dataErr) {
return cb(); self.log.trace({err: dataErr, keyPath: keyPath},
} 'error parsing JSON cache file, removing');
var obj; fs.unlink(keyPath, function (err2) {
try { if (err2) {
obj = JSON.parse(data); self.log.warn({err: err2},
} catch (dataErr) { 'failed to remove JSON cache file');
self.log.warn({err: dataErr, keyPath: keyPath}, }
'error parsing JSON cache file'); cb(err2);
return cb(); });
} return;
cb(null, obj); }
}); cb(null, obj);
}); });
}; };
@ -252,7 +257,7 @@ TritonApi.prototype.getImage = function getImage(name, cb) {
assert.string(name, 'name'); assert.string(name, 'name');
assert.func(cb, 'cb'); assert.func(cb, 'cb');
if (common.UUID_RE.test(name)) { if (common.isUUID(name)) {
this.cloudapi.getImage({id: name}, function (err, img) { this.cloudapi.getImage({id: name}, function (err, img) {
if (err) { if (err) {
cb(err); cb(err);
@ -313,7 +318,7 @@ TritonApi.prototype.getPackage = function getPackage(name, cb) {
assert.string(name, 'name'); assert.string(name, 'name');
assert.func(cb, 'cb'); assert.func(cb, 'cb');
if (common.UUID_RE.test(name)) { if (common.isUUID(name)) {
this.cloudapi.getPackage({id: name}, function (err, pkg) { this.cloudapi.getPackage({id: name}, function (err, pkg) {
if (err) { if (err) {
cb(err); cb(err);