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