Skip to content

Commit

Permalink
Add better error message if context file gets corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Oct 6, 2020
1 parent 5e63471 commit af63687
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,15 @@ function stringify(value) {
return { json: result, circular: hasCircular };
}


function writeFileAtomic(storagePath, content) {
async function writeFileAtomic(storagePath, content) {
// To protect against file corruption, write to a tmp file first and then
// rename to the destination file
let finalFile = storagePath + ".json";
let tmpFile = finalFile + "."+Date.now()+".tmp";
return fs.outputFile(tmpFile, content, "utf8").then(function() {
return fs.rename(tmpFile,finalFile);
})
await fs.outputFile(tmpFile, content, "utf8");
return fs.rename(tmpFile,finalFile);
}

function LocalFileSystem(config){
this.config = config;
this.storageBaseDir = getBasePath(this.config);
Expand All @@ -169,6 +168,7 @@ LocalFileSystem.prototype.open = function(){
if (this.cache) {
var scopes = [];
var promises = [];
var contextFiles = [];
return listFiles(self.storageBaseDir).then(function(files) {
files.forEach(function(file) {
var parts = file.split(path.sep);
Expand All @@ -179,15 +179,22 @@ LocalFileSystem.prototype.open = function(){
} else {
scopes.push(parts[1].substring(0,parts[1].length-5)+":"+parts[0]);
}
promises.push(loadFile(path.join(self.storageBaseDir,file)));
let contextFile = path.join(self.storageBaseDir,file);
contextFiles.push(contextFile)
promises.push(loadFile(contextFile));
})
return Promise.all(promises);
}).then(function(res) {
scopes.forEach(function(scope,i) {
var data = res[i]?JSON.parse(res[i]):{};
Object.keys(data).forEach(function(key) {
self.cache.set(scope,key,data[key]);
})
try {
var data = res[i]?JSON.parse(res[i]):{};
Object.keys(data).forEach(function(key) {
self.cache.set(scope,key,data[key]);
})
} catch(err) {
let error = new Error(log._("context.localfilesystem.invalid-json",{file: contextFiles[i]}))
throw error;
}
});
}).catch(function(err){
if(err.code == 'ENOENT') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
"error-invalid-default-module": "Default context store unknown: '__storage__'",
"unknown-store": "Unknown context store '__name__' specified. Using default store.",
"localfilesystem": {
"invalid-json": "Invalid JSON in context file '__file__'",
"error-circular": "Context __scope__ contains a circular reference that cannot be persisted",
"error-write": "Error writing context: __message__"
}
Expand Down

0 comments on commit af63687

Please sign in to comment.